array of object search
Date tested:
7 years ago
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36
Test name
Executions per second
Array.find
42.2 Ops/sec
indexed copy
351.6 Ops/sec
index map
261.1 Ops/sec
Benchmark definition (click to collapse):
Script Preparation code:
function randstr(N) { return (Math.random().toString(36)+'00000000000000000').slice(2, N+2); } var dataset = []; for (var x = 0; x < 100000; x++) { dataset.push({id: 'xx' + x, name: randstr(10), other: randstr(10)}); } var indexedDataset = {}; for (var item of dataset) { indexedDataset[item.id] = item; } var indexMap = {}; for (var [i, item] of dataset.entries()) { indexMap[item.id] = i; } function assertEq(a, b) { if (a !== b) { new Error(`Failed assert: "${a}" expected "${b}"`); } }
Tests:
Array.find
for (var x = 0; x < 10000; ++x) { const item = dataset.find(v => v.id == 'xx' + 123); assertEq(item.id, 'xx' + 123); }
indexed copy
for (var x = 0; x < 10000; ++x) { const item = indexedDataset['xx' + 123]; assertEq(item.id, 'xx' + 123); }
index map
for (var x = 0; x < 10000; ++x) { const item = dataset[indexMap['xx' + 123]]; assertEq(item.id, 'xx' + 123); }
Open this result on MeasureThat.net