Test name | Executions per second |
---|---|
Array.find, 3 elements | 1176284.6 Ops/sec |
Map.get, 3 elements | 1132418.9 Ops/sec |
Array.find, 10 elements | 685130.9 Ops/sec |
Map.get, 10 elements | 969429.2 Ops/sec |
Clone array 3 | 2325328.5 Ops/sec |
Clone map 3 | 1150065.5 Ops/sec |
Clone array 10 | 2323130.5 Ops/sec |
Clone map 10 | 554955.6 Ops/sec |
function getRandomElement(id) {
return {
id,
a: Math.random(),
b: Math.random(),
c: Math.random(),
}
}
function getArray(length) {
const result = [];
for (let i = 0; i < length; i++) {
result.push(getRandomElement(i))
}
return result;
}
function arrayToMap(array) {
return new Map(array.map(el => [el.id, el]));
}
function cloneArray(array) {
return [array];
}
function cloneMap(map) {
return new Map(map);
}
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
array_small = getArray(3);
array_large = getArray(10);
map_small = arrayToMap(array_small);
map_large = arrayToMap(array_large);
const target = getRandomInt(2);
array_small.find(el => el.id === target);
const target = getRandomInt(2);
map_small.get(target);
const target = getRandomInt(9);
array_large.find(el => el.id === target);
const target = getRandomInt(9);
map_large.get(target)
const array = cloneArray(array_small);
const map = cloneMap(map_small);
const array = cloneArray(array_large);
const map = cloneMap(map_large);