Back to Case Studies
Illustration showing JavaScript array memory layouts and performance metrics.

V8 Arrays Deep Dive: Packed vs Holey

Investigated V8 array internals, benchmarked packed versus holey arrays, and outlined practical patterns to avoid performance pitfalls.

Author: Team ValeffDate: December 12, 2024Time: Not specified5 min read
JavaScriptV8Performance
Share:

The results

A performance-focused breakdown of how V8 stores arrays and why holey arrays can slow access.

9/10

Benchmark cycles where packed arrays were faster

2^20

Array length used in benchmark runs

10

Total performance comparison cycles

How we solved the problem

Context

The article explores a common JavaScript performance confusion: why two arrays with similar values can behave differently at runtime.

The focus is on V8's internal treatment of dense (packed) versus sparse (holey) arrays and their lookup overhead.

The challenge

  • Developers often create sparse arrays accidentally through initialization patterns.
  • Holey arrays can trigger slower property resolution paths than packed arrays.
  • Practical guidance on prevention is often missing in day-to-day codebases.

Execution

1. Explained V8's storage model clearly

  • Compared packed arrays as contiguous value slots.
  • Compared holey arrays as sparse, dictionary-like structures.

2. Mapped element access behavior

  • Walked through bounds checks followed by property checks.
  • Highlighted why missing indices may trigger deeper lookup work.

3. Validated with repeatable benchmarking

  • Generated large packed and holey arrays across multiple cycles.
  • Measured traversal timing and compared run-to-run differences.

4. Translated findings into coding patterns

  • Avoided `new Array(length)` when dense arrays are expected.
  • Preferred explicit values (including undefined) over empty slots.

What changed

  • Packed arrays performed faster in most benchmark cycles (9 out of 10).
  • Initialization patterns were identified as a key root cause of accidental holey arrays.
  • Developers gained a practical framework to balance speed and memory behavior.

What's next

  • Benchmark mutation-heavy workloads beyond read traversal scenarios.
  • Compare behavior across engines to isolate V8-specific assumptions.
    V8 Arrays Deep Dive: Packed vs Holey