let obj = {};
for (let i = 0; i < 100000; i++) {
obj[i] = i;
}
let map = new Map();
for (let i = 0; i < 100000; i++) {
map.set(i, i);
}
let map = new Map();
for (let i = 0; i < 100000; i++) {
map.set(i, i);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Obj / Object.entries() | |
Map / spread | |
Map / Array.from() |
Test name | Executions per second |
---|---|
Obj / Object.entries() | 1494.6 Ops/sec |
Map / spread | 233.8 Ops/sec |
Map / Array.from() | 235.5 Ops/sec |
The benchmark defined tests the performance differences between using JavaScript objects and the Map
data structure. Specifically, it examines how well these structures perform when inserting a series of key-value pairs in different formats.
Test Name: "Obj / Object.entries()"
Benchmark Definition:
let obj = {};
for (let i = 0; i < 100000; i++) {
obj[i] = i;
}
Description: This test creates a plain JavaScript object and populates it with 100,000 key-value pairs, where both the keys and values are the integers from 0 to 99,999. The benchmark likely measures Object.entries()
at a later stage, as it retrieves the entries from the object.
Pros:
obj[i]
).Cons:
Test Name: "Map / spread"
Benchmark Definition:
let map = new Map();
for (let i = 0; i < 100000; i++) {
map.set(i, i);
}
Description: This test uses the Map
object, which is a collection of key-value pairs, where both keys and values can be of any type. The benchmark likely uses the spread syntax to analyze the performance of creating an array from the map.
Pros:
Cons:
Test Name: "Map / Array.from()"
Benchmark Definition:
let map = new Map();
for (let i = 0; i < 100000; i++) {
map.set(i, i);
}
Description: This scenario, like the previous Map
test, explores using Array.from()
to convert the entries of the Map
into an array.
Pros & Cons: Similar to the "Map / spread" test.
Array.from()
allows for flexible transformations but may come with performance overhead compared to the spread syntax depending on the context.From the benchmark results provided, we can analyze how each approach performed in terms of executions per second on the same machine/browser environment.
The object approach significantly outperformed the Map
approaches, demonstrating that for straightforward key-value storage, especially when using integer keys, objects are more efficient. However, the differences in performance for Map
are relatively minor between the two methods of conversion to an array, indicating that either method can be used depending on the developer's preference or specific use case.
WeakMap and WeakSet: These provide a way to create key-value pairs where keys are weakly referenced, meaning they do not prevent garbage collection if there are no other references to the key.
Set: While not strictly a key-value pair structure, a Set
can be used to store unique values and has interesting performance characteristics.
Immutable data structures: Libraries like Immutable.js provide structures that can be more performant for certain types of operations, especially when dealing with large amounts of data that require frequent state changes.
By choosing the right data structure, developers can optimize their applications based on specific needs for performance, memory usage, or ordering behavior.