var items = Array.from(Array(1000), (_, x) => ({
key: x,
value: x * 10
}));
var objContainer = {};
var arrContainer = [];
var mapContainer = new Map();
for (let i = 100; i >= 0; i--) {
const index = Math.floor(Math.random() * 1000);
const item = items[index];
objContainer[item.key] = item;
arrContainer.push(item);
mapContainer.set(item.key, item)
}
items.every(item => objContainer[item.key])
items.every(item => arrContainer.find(containerItem => containerItem.key === item.key))
items.every(item => mapContainer.get(item.key))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object access | |
Array find | |
Map get |
Test name | Executions per second |
---|---|
Object access | 64249944.0 Ops/sec |
Array find | 10818732.0 Ops/sec |
Map get | 49783916.0 Ops/sec |
Let's break down the benchmark and explain what is being tested, along with the pros and cons of each approach.
Benchmark Overview
The benchmark tests three different ways to access an object in JavaScript:
objContainer[item.key]
).find()
method on an array to find an object with a matching key.get()
method on a Map to retrieve an object with a matching key.Pros and Cons of Each Approach
objContainer
with a matching key.mapContainer
with a matching key.Other Considerations
items
array is created using Array.from()
and Array(1000)
, which creates a large array. This may impact performance due to JavaScript's limitations with working with very large arrays.Libraries Used
In this benchmark, no external libraries are used beyond the built-in Map
data structure in JavaScript.
Special JS Features/Syntax
None of the benchmark code uses special JS features or syntax that would affect its interpretation. However, it does use some advanced concepts like arrow functions (() => {}
) and template literals (\r\nitem.key = x * 10\r\n
).
Alternatives
There are several alternatives to these approaches:
Array.find()
for array-based searches, you could consider using a more efficient algorithm like binary search.