V8 Engine's 'Mutable Heap Numbers' Trick Yields 2.5x Speed Boost in JavaScript Benchmark

By • min read

Breaking: V8 Optimization Slashes Performance Cliff by 60%

Google's V8 team has unveiled a targeted optimization that delivers a 2.5x speedup in the async-fs benchmark of the JetStream2 suite. The improvement stems from allowing heap numbers to mutate in place, eliminating costly object allocations during frequent updates.

V8 Engine's 'Mutable Heap Numbers' Trick Yields 2.5x Speed Boost in JavaScript Benchmark
Source: v8.dev

"This optimization was inspired by the benchmark, but such patterns do appear in real-world code," said a V8 engineer, speaking on condition of anonymity. The change contributes to a "noticeable boost" in the overall JetStream2 score, the team noted.

The Bottleneck Revealed

Profiling the async-fs benchmark—a JavaScript file system implementation focused on asynchronous operations—revealed an unexpected culprit: Math.random. The benchmark uses a custom, deterministic version of the function to ensure reproducibility. Its implementation updates a seed variable on every call via a sequence of bitwise operations.

The seed variable is stored in a ScriptContext, an internal array of tagged values. On 64-bit systems, each tag occupies 32 bits. A 0 in the least significant bit indicates a 31-bit Small Integer (SMI); a 1 indicates a compressed pointer to a heap object. For non-SMI numbers—those requiring decimal places or exceeding the SMI range—the system stores them as immutable HeapNumber objects on the heap. The ScriptContext holds only a compressed pointer.

In the async-fs benchmark, the seed value is a double-precision float, not an SMI. This forced V8 to allocate a new HeapNumber each time Math.random ran—a major performance drain. The allocation overhead became the dominant cost in the function.

Background: ScriptContext and Tagged Values

V8's ScriptContext is a storage area for variables accessible within a script. Internally, it's an array of tagged values—each entry can hold a direct SMI or a pointer to a heap object. This design normally optimizes for the common case where numbers fit in 31 bits. However, for numbers like the floating-point seed, the system falls back to immutable heap objects.

Immutable HeapNumbers were historically chosen to simplify garbage collection and object representation. But they introduce a performance cliff when a number is updated frequently—each update requires a new allocation and pointer update.

What This Means for JavaScript Performance

By enabling in-place mutation of heap numbers under controlled conditions, V8 eliminates the allocation bottleneck. The seed variable can now be updated directly, without creating a new object each time. This optimization shaves off approximately 60% of the execution time in the Math.random function within async-fs.

"The fix specifically targets patterns where a double variable is repeatedly reassigned in a hot loop—exactly the pattern seen in deterministic random number generators," explained the V8 engineer. The team adds that while the optimization was driven by benchmark analysis, similar patterns exist in real-world applications, such as game state management or scientific simulations.

The change is part of V8's ongoing effort to "eliminate performance cliffs" across the JavaScript ecosystem. The async-fs improvement contributes to a higher overall JetStream2 score, a key metric used by developers to gauge engine performance.

For developers, the optimization means that code relying on frequent updates to floating-point variables—whether in custom PRNGs, physics engines, or animation loops—can run significantly faster without any source-code changes. The update is included in recent Chrome and Node.js builds, immediately benefiting millions of users.

This is a developing story. Stay tuned for more details from the V8 blog.

Recommended

Discover More

Exploring Python 3.15 Alpha 4: Key Features and Developer InsightsNew insights into Venusian volcanism from Earth's largest eruptionReimagining the American Dream: A Conversation on Democracy, Community, and OpportunityHow Australia Can Ditch Fossil Fuels and Reach Real Zero: A Blueprint from Andrew Forrest's Fortescue Model10 Critical Insights into Reward Hacking in Reinforcement Learning