window.m = new Map();
window.o = {};
for (let i = 100000; i < 100000000; i+=1000) {
var j = Math.floor(Math.random()*999);
window.m.set(i+j, [12345, 67890])
window.o[i+j] = [12345, 67890];
}
for (let i = 0; i < 100000; ++i) {
arrayo = window.m.get(Math.floor(Math.random()*1000000));
}
for (let i = 0; i < 100000; ++i) {
arrayo = window.o[Math.floor(Math.random()*1000000)];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map | |
Object |
Test name | Executions per second |
---|---|
Map | 7.3 Ops/sec |
Object | 7.4 Ops/sec |
Let's dive into the benchmark and explore what's being tested.
Benchmark Overview
The benchmark measures the performance of two approaches to store and retrieve data: Objects (window.o
) and Maps (window.m
). The test case uses a large dataset of random integer keys with corresponding values, which are stored in both objects and maps.
Options Compared
Two options are compared:
window.o
object is populated with 100 million entries, each with a random integer key and a value.window.m
Map is populated with 100 million entries, each with a random integer key and a value.Pros and Cons of Each Approach
Both objects and maps are suitable data structures for storing key-value pairs, but they have different performance characteristics:
In general, maps are faster when dealing with large datasets and multiple lookups, while objects might be preferred when the data structure needs to support other operations like property creation or method calls.
Library Usage
The benchmark uses two libraries:
Math
: The Math
library is used for mathematical functions like floor()
and random()
, which generate random numbers.Special JS Features or Syntax
None of the benchmark code uses any special JavaScript features or syntax, such as async/await, Promises, or Web Workers. It's a simple, straightforward test case that focuses on basic object and map operations.
Alternatives
If you want to create similar benchmarks for other data structures, here are some alternatives:
array[index]
.Keep in mind that each benchmark should be tailored to the specific use case and requirements you want to test.