liveBook · Manning

HeapNumber

As explained in JavaScript in Depth

A V8 internal heap-allocated object that stores a JavaScript number's floating-point representation when the value cannot be represented as an SMI.

Crossing from 2,147,483,647 to 2,147,483,648 can change more than the numeric value: V8 moves from an SMI to a HeapNumber. HeapNumber is a heap-allocated object containing the number’s floating-point representation, with roughly 16 bytes for its header, double, and pointer. This allocation can create a performance and memory cliff in arithmetic-heavy code. section 3.4.2

V8’s simplified storage hierarchy follows these rules:

  • Makes performance depend on engine version, CPU architecture, array size, cache behavior, optimization tier, and surrounding work. section 3.4.3

Keep monetary accumulation integral

Accumulating prices as decimal dollars turns the running total into HeapNumber values during the loop. Accumulating rounded cents keeps the total in the SMI-friendly integer path and converts to a decimal only at the end; the demonstrated integer approach can run at least twice as fast, though actual gains vary. section 3.4.3

How JavaScript in Depth covers it

How deep it goes
Defines allocation and storage differences, then applies them to benchmarks and integer-based optimization without specifying V8 internals beyond the object layout.
Examples
Uses boundary values, timed arithmetic loops, monetary totals in dollars versus cents, and million-element arrays.
What you can do after
Evaluate numeric hot paths and apply integer-scaled accumulation when allocation and predictable performance genuinely matter.
How it connects
Connects HeapNumber with SMI, floating-point representation, heap allocation, arithmetic optimization, and memory usage.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.