Skip to main content

Command Palette

Search for a command to run...

CPU Performance: Objects vs Arrays

Updated
1 min read
CPU Performance: Objects vs Arrays
T

Just a guy who loves to write code and watch anime.

normal JS objects = lots of indirection

when you write:

const pos = { x: 10, y: 20 };

you don’t get one neat chunk of memory with [10,20] side-by-side.

instead:

  • pos is a pointer to an object record.

  • inside that record are pointers to each property’s value (x, y).

  • each value may live somewhere else in memory.

  • there’s metadata for the object’s “shape” (hidden class).

so to access pos.x, the CPU:

  1. follows a pointer to the object

  2. checks its hidden class to know where x lives

  3. follows another pointer to the number value

  4. finally reads the number

that’s a lot of chasing around in memory.


typed arrays = raw, flat memory

when you do:

const pos = new Float32Array([10, 20]);

you literally get:

[ 10.0 | 20.0 ]

just two 32-bit floats back-to-back in one contiguous block of memory.

the CPU can grab both in one go; no metadata, no shape checks, no GC overhead. this layout is exactly what native languages use, and what SIMD loves.