var i = 0, count = 100000, a;
var map = new Map();
var array = [];
var obj = {};
for (i = 0; i < count; i++) {
const randomValue = ~~(Math.random() * 100);
map.set(i, randomValue);
array.push({id: i, randomValue});
obj[i] = randomValue;
}
for (i = 0; i < count; i++) {
a = map.get(i);
}
for (i = 0; i < count; i++) {
a = array.find(el => el.id = i);
}
for (i = 0; i < count; i++) {
a = obj[i]
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
map | |
array | |
obj |
Test name | Executions per second |
---|---|
map | 58.2 Ops/sec |
array | 58.5 Ops/sec |
obj | 61.6 Ops/sec |
Let's dive into the provided benchmark JSON and explore what's being tested.
Benchmark Definition
The benchmark is designed to compare the performance of three data structures: Map
, Array
, and Object
. The script preparation code creates:
Map
instance (map
) with 100,000 entries.Array
instance (array
) with 100,000 elements.Object
instance (obj
) with 100,000 properties.The loop iterates 100,000 times, and in each iteration:
get()
method.find()
method with a callback function that checks if the element's id
property matches the current index (i
).obj[i]
).Options being compared
The three options being compared are:
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Map
, especially for large datasets or sparse data.Map
for large datasets, as it uses a hash table under the hood.Library usage
None of the benchmark cases use any external libraries or dependencies.
Special JS features or syntax
The benchmark makes use of some JavaScript features:
Other alternatives
If you'd like to explore alternative data structures or approaches, here are some options:
Map
, but without keys.Keep in mind that each data structure has its strengths and weaknesses, and the best choice depends on the specific use case and performance requirements.