Learn JavaScript

Lesson 5 of 9 · Arrays and the Array Methods: The map, filter, reduce Pipeline

Module 7 · Arrays and the Array Methods: The map, filter, reduce Pipeline

Under the Hood: Arrays as Objects, V8's Elements Kinds, and What Holes, shift and Spread Cost

ProReading

Maria asks "but why" more than anyone in the class. In Lesson 02 she read that shift is O(n) and push is O(1), and she wants to see it, not take it on trust. She also wants to know why typeof [] says "object". This lesson answers both by looking at two layers. One is what the JavaScript specification promises. The other is what V8, the engine inside Node 22 and Chrome, actually does.

Checking what this lesson needs for you…

What is inside Under the Hood: Arrays as Objects, V8's Elements Kinds, and What Holes, shift and Spread Cost

  1. An array is an object with numeric keys
  2. V8's elements kinds: packed and holey
  3. What a hole really costs
  4. Why shift is O(n)
  5. The copy cost of spread