Object key vs array lookup

Benchmark created on


Setup

// Tiny
var tinyObj = {};
var tinyArray = [];
for (let i = 0; i < 10; i++) {
  tinyObj[i] = i;
  tinyArray.push(i);
}

// Small
var smallObj = {};
var smallArray = [];
for (let i = 0; i < 100; i++) {
  smallObj[i] = i;
  smallArray.push(i);
}

// Medium
var mediumObj = {};
var mediumArray = [];
for (let i = 0; i < 1000; i++) {
  mediumObj[i] = i;
  mediumArray.push(i);
}

// Large
var largeObj = {};
var largeArray = [];
for (let i = 0; i < 10000; i++) {
  largeObj[i] = i;
  largeArray.push(i);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Tiny Object
tinyObj['5']
ready
Tiny Array
// Small Array Looping
for (let i = 0; i < tinyArray.length; i++) {
  if (tinyArray[i] === 5) {
    break;
  }
}
ready
Small Object
smallObj['50']
ready
Small Array
// Small Array Looping
for (let i = 0; i < smallArray.length; i++) {
  if (smallArray[i] === 50) {
    break;
  }
}
ready

Revisions

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