Typed Array vs Untyped Array vs Object for key-value access

Benchmark created on


Setup

const emptyArray = [];
const emptyArrayWithFoo =[];
emptyArrayWithFoo.foo='some other data';
const emptyObject = {};
const emptyObjectWithFoo = {};
emptyObjectWithFoo.foo = 'some other data';

const arrLength = 1e6;
const typedArray = new Uint32Array(arrLength);
const preAllocatedArray = Array(arrLength ).fill(0);
const preAllocatedArrayWithFoo = Array(arrLength ).fill(0);
preAllocatedArrayWithFoo.foo = 'some other data';

const preAllocatedObj = Object.fromEntries(Object.entries(preAllocatedArray ));
const preAllocatedObjWithFoo = Object.fromEntries(Object.entries(preAllocatedArray ));
preAllocatedObjWithFoo.foo = 'some other data';

function rand(){return Math.random()*arrLength | 0}

Test runner

Ready to run.

Testing in
TestOps/sec
Empty Array
emptyArray[rand()] = emptyArray[rand()]||0+1;
ready
Pre Allocated Array
preAllocatedArray[rand()] = preAllocatedArray[rand()]||0+1;
ready
Empty Array with Extra Field
emptyArrayWithFoo[rand()] = emptyArrayWithFoo[rand()]||0+1;
ready
Pre Allocated Array with Extra Field
preAllocatedArrayWithFoo[rand()] = preAllocatedArrayWithFoo[rand()]||0+1;
ready
Typed Array (must be pre allocated)
typedArray[rand()] = typedArray[rand()]||0+1;
ready
Empty Object
emptyObject[rand()] = emptyObject[rand()]||0+1;
ready
Pre Allocated Object
preAllocatedObj[rand()] = preAllocatedObj[rand()]||0+1;
ready
Empty Object with Extra Field
emptyObjectWithFoo[rand()] = emptyObjectWithFoo[rand()]||0+1;
ready
Pre Allocated Object with Extra Field
preAllocatedObjWithFoo[rand()] = preAllocatedObjWithFoo[rand()]||0+1;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.