---
title: "Turbocharging V8 with mutable heap numbers"
description: "V8 eliminated a performance cliff in JetStream2's async-fs benchmark caused by a custom Math.random whose `seed` lives in a ScriptContext slot."
url: "https://swe.quest/articles/turbocharging-v8-with-mutable-heap-numbers-d32025b1d5ea1f1c"
canonical: "https://v8.dev/blog/mutable-heap-number"
source: "V8"
published: "2025-02-25"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 5
tags: ["runtimes","compilers","javascript","performance","memory-management"]
---

# Turbocharging V8 with mutable heap numbers

A post on [V8](https://swe.quest/sources/v8.md), published 2025-02-25, 5 min read, Deep dive. Original: [https://v8.dev/blog/mutable-heap-number](https://v8.dev/blog/mutable-heap-number)

## Summary

V8 eliminated a performance cliff in JetStream2's async-fs benchmark caused by a custom Math.random whose \`seed\` lives in a ScriptContext slot. Every update to the seed allocated a new immutable HeapNumber, and the integer bitwise math was compiled as floating-point. V8 now tracks slot types (constant, SMI, HeapNumber, Int32, other) and lets a context slot own a mutable HeapNumber that is updated in place, storing raw Int32 values when they stay in range. Optimized code depends on the slot type staying stable and deoptimizes if it changes, and the change gives about a 2.5x speedup on async-fs and about 1.6% on the overall JetStream2 score.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Runtimes & VMs](https://swe.quest/tags/runtimes.md) (Compilers & runtimes)
- [Compilers](https://swe.quest/tags/compilers.md) (Compilers & runtimes)
- [JavaScript](https://swe.quest/tags/javascript.md) (Languages)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Memory management](https://swe.quest/tags/memory-management.md) (Compilers & runtimes)

## Prerequisites

- JIT compilation and deoptimization ([easy posts on Compilers](https://swe.quest/tags/compilers.md?difficulty=easy))
- V8 tagged values and SMIs ([easy posts on Runtimes & VMs](https://swe.quest/tags/runtimes.md?difficulty=easy))
- pointer compression

## Keywords

V8, JetStream2, HeapNumber, ScriptContext, async-fs, Math.random

## Related posts

- [V8 release v6.9 · V8](https://swe.quest/articles/v8-release-v6-9-v8-d0efd26e2594b2df.md): V8 · 2018-08-07 · Easy · 4 min read · Runtimes & VMs, JavaScript, Memory management, WebAssembly, Compilers
  V8 v6.9 embeds built-in functions directly into the binary on x64, so all Isolates share one copy instead of each holding up to 700 KB on its own heap.
- [Celebrating 10 years of V8 · V8](https://swe.quest/articles/celebrating-10-years-of-v8-v8-26690aa14d628d44.md): V8 · 2018-09-11 · Easy · 9 min read · Runtimes & VMs, JavaScript, Compilers, Memory management, Performance engineering
  V8 grew over ten years from a secret project started in 2006 on a Danish farm into a JavaScript and WebAssembly engine supporting eight instruction sets, embedded by both Chrome and Node.js.
- [How Bun supports V8 APIs without using V8 (part 2)](https://swe.quest/articles/how-bun-supports-v8-apis-without-using-v8-part-2-d765ff746fe7a86a.md): Bun · 2024-11-05 · Hard · 18 min read · Runtimes & VMs, JavaScript, C & C++, Memory management
  Bun emulates V8's memory layout on top of JavaScriptCore so that precompiled native addons, which inline V8 functions assuming specific pointer layouts, keep working.
- [The V8 Sandbox](https://swe.quest/articles/the-v8-sandbox-705346b1934809ad.md): V8 · 2024-04-04 · Hard · 11 min read · Sandboxing & isolation, Runtimes & VMs, Memory safety, Browsers, Vulnerability research
  The V8 Sandbox is a lightweight, in-process software sandbox that isolates the V8 heap so memory corruption from engine bugs cannot spread to the rest of the renderer process.
- [Static Roots: Objects with Compile-Time Constant Addresses](https://swe.quest/articles/static-roots-objects-with-compile-time-constant-addresses-91e4d5de9f87687b.md): V8 · 2024-02-05 · Hard · 4 min read · Runtimes & VMs, Memory management, Performance engineering, JavaScript, C & C++
  V8's static roots place the read-only heap at the start of every pointer-compression cage and generate it deterministically, so core objects like \`undefined\` get compressed addresses known at compile time (e.g. \`undefined\` is always 0x61).

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
