Object key vs array index vs Map() vs ArrayBuffer() lookup

Benchmark created on


Setup

// Setup
const SIZE = 10000; // Size of the array and object
let testArray = [];
let testObject = {};
let testMap = new Map();

// Array buffer
const buffer = new ArrayBuffer(SIZE * Int32Array.BYTES_PER_ELEMENT);
const intArray = new Int32Array(buffer);

// Initialize the array and object with the same data
for (let i = 0; i < SIZE; i++) {
    testArray.push(i);
    testObject[i] = i;
    testMap.set(i, i);
    intArray[i] = i; 
}

// Generate a random index for lookup
const randomIndex = Math.floor(Math.random() * SIZE);

Test runner

Ready to run.

Testing in
TestOps/sec
Array Index Lookup
let arrayValue = testArray[randomIndex];
ready
Object Key Lookup
let objectValue = testObject[randomIndex];
ready
Array Buffer Lookup
let intArrayValue = intArray[randomIndex];
ready
Map Lookup
let mapValue = testMap.get(randomIndex);
ready

Revisions

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