window.m = new Map();
window.o = {};
for (let i = 0; i < 2000; ++i) {
window.m.set(Math.random(100000) , i);
window.o[Math.random(100000)] = i;
}
for (let i = 0; i < 10000; ++i) {
if (window.m.get( Math.random(10000)) !== 1) {
}
}
for (let i = 0; i < 10000; ++i) {
if (window.o[Math.random(10000)] !== 1) {
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map Lookup | |
Object Lookup |
Test name | Executions per second |
---|---|
Map Lookup | 666.7 Ops/sec |
Object Lookup | 183.3 Ops/sec |
Let's break down the provided benchmark and its components.
Benchmark Definition
The benchmark defines two test cases: Object Lookup and Map Lookup. Both tests aim to compare the performance of looking up values in large objects versus large maps when using integers as keys.
Options Compared
Two options are compared:
window.o
) to store integer values, where each key is generated randomly.window.m
) to store the same integer values, where each key is also generated randomly.Pros and Cons of Each Approach
Library Used
Neither object lookup nor map lookup explicitly use a library. However, the window
object is used, which is a built-in JavaScript object that provides access to various global variables, functions, and properties.
Special JS Feature or Syntax
There are no special features or syntaxes mentioned in this benchmark. The focus is on comparing the performance of different data structures (objects vs maps) for lookup operations.
Other Considerations
When designing benchmarks like this, consider the following:
Alternative Benchmarks
Other alternatives to measure object vs map lookup performance might include:
Keep in mind that different benchmarks might be more relevant depending on your specific use case, application, or industry requirements.