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 getArrays(length) {
const result = new Array(length).fill(0);;
for (let i = 0; i < length; i++) {
result[i] = (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 = getArrays(100);
array_large = getArrays(10000);
map_small = arrayToMap(array_small);
map_large = arrayToMap(array_large);
const target = getRandomInt(99);
array_small.find(el => el.id === target);
const target = getRandomInt(99);
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, 100 elements | |
Map.get, 100 elements | |
Array.find, 1 000 000 elements | |
Map.get, 1 000 000 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, 100 elements | 10705738.0 Ops/sec |
Map.get, 100 elements | 58079804.0 Ops/sec |
Array.find, 1 000 000 elements | 29872.3 Ops/sec |
Map.get, 1 000 000 elements | 60034052.0 Ops/sec |
Array to Map + Map.get, 1 lookup | 299120.8 Ops/sec |
Array to Map + Map.get, 10 lookups | 294243.6 Ops/sec |
Array to Map + Map.get, 100 lookups | 234919.8 Ops/sec |
Array.find, 10 lookups | 683254.4 Ops/sec |
Array.find, 100 lookups | 67373.5 Ops/sec |
This benchmark evaluates the performance of two different methods used for searching or retrieving values from a collection in JavaScript: Array.find
and Map.get
. It compares these methods when applied to arrays of varying sizes and when using intermediate data structures.
Array.find
id
).Map.get
Array.find
, particularly for larger collections.The benchmark includes multiple test cases to illustrate performance under different conditions:
Array.find
and Map.get
.Map.get
after converting a small array to a map, for scenarios with varying numbers of lookups (1, 10, and 100 lookups).Pros:
Cons:
Pros:
Cons:
arrayToMap
function transforms an array of objects into a Map format, making retrieval operations faster.The benchmark results show that Map.get
significantly outperforms Array.find
in nearly all scenarios, especially with larger arrays:
Map.get
executed approximately 60 million times per second, while Array.find
struggled around 29,000 times per second.Map.get
shows the efficacy of the map structure in enabling quick retrievals, while Array.find
shows increasingly slower results as the number of lookups increases.Other potential alternatives for optimized lookups might include:
The choice between these data structures heavily relies on the specific needs of the application and the nature of the data being managed. Each structure offers unique trade-offs between complexity, performance, and ease of use.