var map = new Map();
var obj = {};
var obj2 = Object.create(null);
for (let i = 0; i < 10000; i++) {
map.set(`a${i}`, i);
}
for (let i = 0; i < 10000; i++) {
obj[`a${i}`] = i;
}
for (let i = 0; i < 10000; i++) {
obj2[`a${i}`] = i;
}
let map = new Map();
for (let i = 0; i < 10000; i++) {
map.delete(`a${i}`);
}
for (let i = 0; i < 10000; i++) {
delete obj[`a${i}`];
}
for (let i = 0; i < 10000; i++) {
delete obj2[`a${i}`];
}
for (let i = 0; i < 10000; i++) {
const map2 = new Map(map);
}
for (let i = 0; i < 10000; i++) {
const objCopy = {obj};
}
for (let i = 0; i < 10000; i++) {
const objCopy = {obj2};
}
for (let i = 0; i < 10000; i++) {
const newObj = Object.fromEntries(map.entries());
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map Insert | |
Object Insert | |
Object2 Insert | |
Map Delete | |
Object Delete | |
Object2 Delete | |
Map Copy | |
Object Copy | |
Object2 Copy | |
Map to Object |
Test name | Executions per second |
---|---|
Map Insert | 1229.8 Ops/sec |
Object Insert | 1052.9 Ops/sec |
Object2 Insert | 1053.2 Ops/sec |
Map Delete | 5714.5 Ops/sec |
Object Delete | 1046.2 Ops/sec |
Object2 Delete | 1353.9 Ops/sec |
Map Copy | 0.2 Ops/sec |
Object Copy | 875.2 Ops/sec |
Object2 Copy | 880.7 Ops/sec |
Map to Object | 0.1 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this particular benchmark.
Benchmark Overview
This benchmark compares the performance of three data structures: Object
, Map
, and Object2
(a variant of Object
). Each test case measures the execution time of various operations:
The benchmarks aim to determine which data structure is most efficient for these common use cases.
Data Structures
Let's take a closer look at each data structure being compared:
obj
variable.map
variable. Maps are similar to objects but offer additional methods and features like key-value pairs and efficient iteration.obj2
variable. This variant is not a standard JavaScript object.Benchmark Operations
Each test case measures the execution time for one or more operations:
Object
insertion: obj[key] = value
Map
insertion: map.set(key, value)
Object2
insertion: obj2[key] = value
(assuming a custom implementation)Object
deletion: delete obj[key]
Map
deletion: map.delete(key)
Object
copying: const copiedObj = { ... obj }
Map
copying: const copiedMap = new Map([...map])
Map to Object
: Converting a Map
to an equivalent Object
. This operation is not directly applicable to Object2
.Performance Comparison
The benchmark outputs the execution time for each test case, providing insights into which data structure performs best under various scenarios.
In this benchmark:
Object
and Object2
in terms of insertion, deletion, and copying operations.Object
.Keep in mind that these results may depend on specific use cases and implementation details. The benchmark is likely designed to highlight the strengths of JavaScript's built-in Map
data structure.
Please let me know if you have any further questions or if there's anything else I can help with!