The same scheduling code can print a different result in Node.js and Deno, even though both use V8. V8 is the JavaScript engine; the host runtime also supplies APIs and timing behavior. Sharing an engine does not make hosts interchangeable. section 1.1
ECMAScript defines observable behavior, not how an engine must store values or execute code. V8 exploits that freedom to optimize common cases. Its implementation details explain performance surprises, but they are not guarantees for every JavaScript engine. section 3.4 section 5.10
Several ordinary JavaScript values have specialized V8 representations:
- Strings are heap objects. Small strings occupy regular heap pages; sufficiently large strings use Large Object Space. ConsStrings can represent concatenation through references rather than immediate copying. section 2.2.2 section 2.3
- Numbers must behave as 64-bit floating-point values, but V8 can optimize small-integer arithmetic. Its BigInts instead use multiple 64-bit chunks when needed. section 3.4 section 3.2
- Predictable object structures and access patterns help property-access optimization. Objects that switch to a slower mode, or fail to share hidden classes, can incur substantial costs. section 5.10
Functions have a separate performance story. V8 initially pre-parses a function body; it finishes parsing and generates bytecode when the function is called. Called code can later be optimized or deoptimized according to its observed execution. A function's first call therefore pays work that an uncalled function never does. section 6.6 section 6.6.1
A cold branch, not a forbidden catch
Call a range-checking function thousands of times with valid inputs, then pass an out-of-range value. V8 may deoptimize when execution enters the previously unvisited catch path. The cause is the newly taken branch, not a general inability to optimize try-catch. Creating an Error has a separate cost: it records stack-frame information even if its stack property is never read. section 7.4.1
Figure 9.3 V8 divides its heap into distinct spaces. The young generation contains two semi-spaces (from-space and to-space), only one active at a time. Objects that survive two scavenges are promoted to the old generation. Allocations too large for the young generation go directly to the large object space.
The heap diagram separates the young generation's two semi-spaces, the old generation, and Large Object Space. It makes clear why object lifetime and size affect collection differently. section 9.2.1
For garbage-collection performance, ask what remains reachable rather than merely counting allocations. Short-lived objects can be cheap; surviving objects extend the collector's work. Use --trace-gc to check whether collection is actually a problem before changing code. section 9.7
