Memory Ram architecture
Fixed Size data: Stack, dynamic size: Heap
4 bit architecture
Garbage Collector
Chart table
| Unit |
Symbol |
Conversion |
In Bytes |
| Byte |
B |
Base unit |
1 B |
| Kilobyte |
KB |
1 KB = 1024 B |
2¹⁰ B |
| Megabyte |
MB |
1 MB = 1024 KB |
2²⁰ B = 1,048,576 B |
| Gigabyte |
GB |
1 GB = 1024 MB |
2³⁰ B = 1,073,741,824 B |
| Terabyte |
TB |
1 TB = 1024 GB |
2⁴⁰ B ≈ 1.1 trillion B |
| Petabyte |
PB |
1 PB = 1024 TB |
2⁵⁰ B ≈ 1.13 quadrillion B |
| Exabyte |
EB |
1 EB = 1024 PB |
2⁶⁰ B ≈ 1.15 quintillion B |
| Zettabyte |
ZB |
1 ZB = 1024 EB |
2⁷⁰ B ≈ 1.18 sextillion B |
| Yottabyte |
YB |
1 YB = 1024 ZB |
2⁸⁰ B ≈ 1.21 septillion B |
Where the Data is actually stored?
The Two Memory Regions in V8
When your JavaScript code runs, the V8 engine manages two primary areas of memory:
- The Call Stack: A highly organized region for managing function execution. It's fast and stores fixed-size data. On a 32-bit system, the "slots" on the stack are 32 bits (4 bytes) each.
- The Heap: A large, less organized region for storing data that can change in size or has a longer lifetime. This is where objects, arrays, and other complex data live.
How Primitive Data is Stored (The V8 Reality)
This is where V8's optimizations are critical. It does not follow a simple "all primitives on the stack" rule.
number (The Hybrid Approach)
V8 splits numbers into two types to maximize performance:
-
Smi (Small Integer): code JavaScript
- What it is: A 31-bit signed integer. This covers most numbers you use daily (from approx. -1 billion to +1 billion).
- How it's stored: V8 uses a technique called Pointer Tagging. A 32-bit slot on the stack is used. The very last bit is a "tag." If this bit is 0, V8 knows the other 31 bits are an actual integer value.
- Where it's stored: Directly on the Stack. A Smi is a true primitive in the classic sense. It is never allocated on the heap. This makes integer arithmetic extremely fast.
let loopCounter = 100; // This is a Smi. Its value lives on the stack.
-
HeapNumber: code JavaScript
- What it is: Any number that is not a Smi. This includes all floating-point numbers (e.g., 19.5) and integers too large to be a Smi.
- How it's stored: The value is "boxed" into a special object on the Heap. This object holds the full 64-bit (8-byte) representation of the number.
- The variable on the Stack holds a 32-bit pointer (a memory address) to this object on the Heap. The tag bit for this pointer will be 1.
let price = 19.99; // This is a HeapNumber. 'price' on the stack is a pointer to an object on the heap.