window.myMap = new Map();
window.myObj = new Map();
window.generateObj = () => ({
'a': Math.random() * 10000,
'b': Math.random() * 10000,
'c': Math.random() * 10000
});
for (let i = 0; i < 1000; i++) {
let id = 'id-' + i;
let obj = generateObj();
myMap.set(id, obj);
myObj[id] = obj;
}
let id = `id-` + Math.floor(Math.random() * 1000);
let value = myMap.get(id);
let id = `id-` + Math.floor(Math.random() * 1000);
let value = myObj[id];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Read from map | |
Read from obj |
Test name | Executions per second |
---|---|
Read from map | 1749678.5 Ops/sec |
Read from obj | 1403757.0 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Definition
The benchmark measures the performance of reading values from two data structures: Map
and Object
. Both data structures are created with string keys, but they differ in how the keys are generated. The key generation is done using template literals (id-
+ random number).
Options Compared
There are two options compared:
generateObj()
that returns an object with three properties (key-value pairs). This object is then set on the Map
instance using the generated key.generateObj()
function is used, but its return value is assigned directly to the Object
instance using the generated key.Pros and Cons of Each Approach
Map
Pros:
Cons:
get()
returns undefined
.Object
Pros:
Cons:
Library and Special JS Features
The Map
and Object
instances are part of the JavaScript standard library. No special libraries or features are used in this benchmark.
Other Considerations
This benchmark is likely intended to test the performance differences between maps and objects when it comes to fast lookups. The use of template literals and random key generation adds an extra layer of complexity, making it a more challenging benchmark.
If you want to simulate similar scenarios, you can create additional benchmark definitions with different options (e.g., reading from arrays or sets) to test the performance of various JavaScript data structures.
Other alternatives for measuring performance in JavaScript benchmarks include:
Date.now()
and manual timing loops.performance.now()
or requestAnimationFrame()
.react-dom/test-utils
or Angular's @angular/core/testing
.