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 getRandomInt(max) {
return Math.floor(Math.random() * max);
}
array_small = getArray(10);
array_large = getArray(100);
map_small = arrayToMap(array_small);
map_large = arrayToMap(array_large);
const target = getRandomInt(9999);
array_small.find(el => el.id === target);
const target = getRandomInt(9999);
map_small.get(target);
const target = getRandomInt(999999);
array_large.find(el => el.id === target);
const target = getRandomInt(999999);
map_large.get(target)
const map = arrayToMap(array_small);
const target = getRandomInt(9999);
map.get(target);
const map = arrayToMap(array_small);
for (let i = 0; i < 10; i++) {
const target = getRandomInt(9999);
map.get(target);
}
const map = arrayToMap(array_small);
for (let i = 0; i < 100; i++) {
const target = getRandomInt(9999);
map.get(target);
}
for (let i = 0; i < 10; i++) {
const target = getRandomInt(9999);
array_small.find(el => el.id === target)
}
for (let i = 0; i < 100; i++) {
const target = getRandomInt(9999);
array_small.find(el => el.id === target)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.find, 10 elements | |
Map.get, 10 elements | |
Array.find, 100 elements | |
Map.get, 100 elements | |
Array to Map + Map.get, 1 lookup | |
Array to Map + Map.get, 10 lookups | |
Array to Map + Map.get, 100 lookups | |
Array.find, 10 lookups | |
Array.find, 100 lookups |
Test name | Executions per second |
---|---|
Array.find, 10 elements | 55705348.0 Ops/sec |
Map.get, 10 elements | 111301880.0 Ops/sec |
Array.find, 100 elements | 10466441.0 Ops/sec |
Map.get, 100 elements | 106299496.0 Ops/sec |
Array to Map + Map.get, 1 lookup | 4338972.5 Ops/sec |
Array to Map + Map.get, 10 lookups | 3121109.2 Ops/sec |
Array to Map + Map.get, 100 lookups | 785736.2 Ops/sec |
Array.find, 10 lookups | 3831773.2 Ops/sec |
Array.find, 100 lookups | 416246.9 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares the performance of two approaches: Array.find
and Map.get
. The test cases are designed to measure the execution time of each approach for small-scale (10 elements) and large-scale (100 elements) datasets.
Options Compared
There are four options compared:
true
when the desired element is found.Map.get
to retrieve the values.Test Cases
The benchmark includes six test cases:
What's Being Tested
The benchmark is testing the performance of each approach in terms of execution time. The results indicate that:
Map.get
performs significantly better than Array.find
for both small-scale and large-scale datasets.Performance Analysis
The benchmark results show that Map.get
outperforms Array.find
by orders of magnitude, even for small-scale datasets. This is because Map.get
uses a hash table lookup, which has an average time complexity of O(1), whereas Array.find
uses linear search, which has a worst-case time complexity of O(n).
The Array to Map + Map.get approach shows improved performance compared to Array.find
, especially for large-scale datasets. This is likely due to the caching benefits provided by the Map data structure.
Overall, the benchmark highlights the importance of using optimized data structures and methods when working with arrays or maps in JavaScript.