DEV Community

Discussion on: Javascript academy #1: Primitive value vs reference value

Collapse
akashkava profile image
Akash Kava

In JavaScript, primitive means immutable, they don’t live on stack. String is never put on stack. In V8 there are objects such as V8Number etc which are primitive but do not live on stack.

V8 tries to optimize code by using short circuit variables on stack such as creating two numbers and adding them. However if number comes as an input it will be on heap.

In high level languages, unless they are strictly typed, it is impossible to put primitives on stack as there would be too much boxing required which will make code slower to generate and execute.

Collapse
codeoz profile image
CodeOz Author

Primitive value are immutable as you said, the variable will store this value inside it, but this variable live in the stack (variable that store primitive or reference value, but for reference value, the variable store the reference of the object in the stack but the object is store into the Heap, and you get the object thanks to this reference into the Heap).

I don't understand when you speak about V8Number.

If you want to check some others source:

Collapse
akashkava profile image
Akash Kava • Edited on

In the same Stackoverflow link given in the last this is the line.

Well for starters we're talking about JavaScript, and JavaScript doesn't have a stack or a heap. It's a dynamic language and all the variables in JavaScript are dynamic. To explain the difference I'll compare it to C....

I have integrated V8 for Android at github.com/web-atoms/xamarin-v8 and I can guarantee, that nothing except small integer (31 bits, one bit to differentiate heap allocated number) lives in the stack, V8Number is the exact class which is allocated on the heap which stores the value, all JavaScript values are on heap and they are all derived from V8Value class, please refer to the source code of V8.

Now, v8 analyzes source code and does some shortcut, like adding two numbers,

function add(a) {
   var c = 2;
   return a + c;
}
Enter fullscreen mode Exit fullscreen mode

In Above function, a is unknown, so it is of type V8Value in C++. However, V8 sees that we are adding literal 2 which is kept in variable c, so V8 intelligently puts 2 in stack and then combines it with a. As long as compiler can track the type, it can keep it on stack, otherwise it needs to create reference. As everything passed as an argument to other method in C++ are handled by references.

So only for certain operations V8 puts things on stack, otherwise everything lives in heap, when you call a method add(2, 4) , all though you know that you are passing 2 and 4 but function doesn't , everything is dynamic. So compiler has to allocate V8Number, and pass reference to add method.

To understand how complex it is to put primitives on stack, try creating a small javascript engine and you will see how it is possible and how it is complex enough to not do it.

Some comments have been hidden by the post's author - find out more