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(2);
array_large = getArray(5);
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 000 elements | |
Map.get, 10 000 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, 10 000 elements | 30334578.0 Ops/sec |
Map.get, 10 000 elements | 1126225920.0 Ops/sec |
Array.find, 1 000 000 elements | 15867060.0 Ops/sec |
Map.get, 1 000 000 elements | 1099068160.0 Ops/sec |
Array to Map + Map.get, 1 lookup | 1876891.1 Ops/sec |
Array to Map + Map.get, 10 lookups | 1894195.1 Ops/sec |
Array to Map + Map.get, 100 lookups | 1637980.6 Ops/sec |
Array.find, 10 lookups | 3035343.0 Ops/sec |
Array.find, 100 lookups | 236440.8 Ops/sec |
Let's break down the test cases and explain what is being tested.
Overview
The test cases compare the performance of two approaches: Array.find
and Map.get
. The tests are designed to measure the execution time of these methods for different scenarios, such as varying array sizes and the number of lookups.
What is being compared?
Options compared
The tests compare the performance of these two methods under different conditions:
Test cases
Each test case measures the execution time of one of the two methods under the specified conditions. The results are reported as executions per second.
For example, the first test case measures the execution time of Array.find
for a small array with 10,000 elements.
Interpretation of results
The test results show that:
Map.get
is generally faster than Array.find
, especially for large arrays.Map.get
remains faster.Array.find
may be slightly faster due to caching or other optimizations.Device and OS
All tests are run on a desktop device with an Ubuntu operating system and running Firefox 117. The results are reported for this specific configuration.
Conclusion
The test cases aim to measure the performance difference between Array.find
and Map.get
under various conditions, providing insights into which method is more efficient in different scenarios.