window.m = new Map();
window.o = {};
for (let i = 0; i < 2000; ++i) {
window.m.set(Math.random(100000), [i, i + 1]);
window.o[Math.random(100000)] = [i, i + 1];
}
for (let i = 0; i < 10000; ++i) {
if (window.m.get( Math.random(100000)) !== 1) {
}
}
for (let i = 0; i < 10000; ++i) {
if (window.o[Math.random(100000)] !== 1) {
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map Lookup | |
Object Lookup |
Test name | Executions per second |
---|---|
Map Lookup | 270.8 Ops/sec |
Object Lookup | 81.2 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark is testing two different approaches to lookup performance in JavaScript:
Map
data structure to store key-value pairs. The map is populated with random integers as keys and arrays containing two values (i
and i+1
) as values.window.o = {}
) to store similar key-value pairs.Comparison
The benchmark is comparing the performance of these two approaches on a large dataset (2000 entries). The test measures the execution time for each approach over 10,000 iterations.
Options Compared
There are two main options being compared:
Map
data structure to store key-value pairs.window.o = {}
) to store key-value pairs.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Map
objects (most modern browsers do).Map
due to the need for linear searching.Library Used
The Map
object is used as part of the standard library in most modern JavaScript engines, including those used by Google Chrome. The Array.prototype.includes()
method is also used in both benchmarks.
Special JS Feature/Syntax
There are no special JavaScript features or syntax mentioned in this benchmark. However, it's worth noting that using a Map
object requires support for the Map
data structure, which was introduced in ECMAScript 2015 (ES6).
Other Alternatives
If you want to explore alternative approaches, here are a few options:
Keep in mind that these alternatives may have different performance characteristics or compatibility issues compared to using Map
objects.