How does V8 store integers like 5?
javascript, memory, memory-management, v8
Solution
V8 uses a pointer tagging scheme to distinguish small integers and heap object pointers. 5 would be stored as a `Smi` type, which is not heap allocated in V8.
You can check out the source code for the Smi class to learn more.
On 32-bit platforms, Smis are a 31 bit signed int with a 0 set for the bottom bit. On 64-bit platforms, Smis are a 32 bit signed int, 31 bits of 0 padding and a 0 for the bottom bit. Pointers to heap objects have a 1 set for the bottom bit so that V8 can tell the difference between pointers and Smis without extra metadata.
Problem
How does V8 store integers in memory? For example the integer 5? I know it stores it the heap, but how exactly does it store it? Things like metadata and the actual value itself. Is there a constant added to the int before storing it?