window.m = new Map();
window.o = {};
for (let i = 0; i < 1000; ++i) {
window.m.set('a' + i, i);
window.o['a' + i] = i;
}
for (let i = 0; i < 1000; ++i) {
if (window.m.get('a' + i) !== i) {
console.log("bad map key");
}
}
for (let i = 0; i < 1000; ++i) {
if (window.o['a' + i] !== i) {
console.log("bad object key");
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map Lookup | |
Object Lookup |
Test name | Executions per second |
---|---|
Map Lookup | 5244.6 Ops/sec |
Object Lookup | 4305.3 Ops/sec |
Let's break down the provided benchmark definition and test cases to understand what's being tested.
Benchmark Definition: The benchmark defines two tests:
window.m
) and populates it with 1000 key-value pairs using set()
. It then iterates over the same range, checking if each value can be retrieved from its corresponding key in the map using get()
. If any retrieval fails, an error message is logged to the console.window.o
) and populates it with 1000 key-value pairs using string keys and values. It then iterates over the same range, checking if each value can be retrieved from its corresponding key in the object using bracket notation (o['a+i']
). If any retrieval fails, an error message is logged to the console.Libraries: Both tests use built-in JavaScript objects and methods:
Map
: A dictionary-like data structure that stores key-value pairs.Object
: An associative array-like data structure that stores key-value pairs using string keys.console.log()
, console.error()
: The global logging functions in the browser console.Special JS Features/Syntax: None of these tests use special JavaScript features or syntax beyond what's standard in modern JavaScript. They rely on basic object manipulation and iteration techniques.
Options Compared: The two tests compare the performance of:
Map
data structure to store and retrieve values.Object
data structure with string keys to store and retrieve values.Pros and Cons:
get()
.undefined
.Considerations:
Alternatives:
Other data structures or methods that could be used instead of Map
or Object
include:
However, these alternatives would likely require significant changes to the benchmark definition and test cases to accurately compare their performance.