<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/immutability-helper@2.7.0/index.min.js"></script>
let obj = {};
for(i=0;i<100;i++){
const key = 'key'+i
const value = 'value'+i
obj = {obj, [key]: {key, value}}
}
let obj = Immutable.Map();
for(i=0;i<100;i++){
const key = 'key'+i
const value = 'value'+i
obj = obj.set(key, {key, value})
}
let obj = new Map();
for(i=0;i<100;i++){
const key = 'key'+i
const value = 'value'+i
obj.set(key, {key, value})
}
let obj = {};
for(i=0;i<100;i++){
const key = 'key'+i
const value = 'value'+i
obj[key] = {key, value};
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
object spread | |
immutable-js | |
native Map | |
Object |
Test name | Executions per second |
---|---|
object spread | 6945.3 Ops/sec |
immutable-js | 15497.3 Ops/sec |
native Map | 50121.5 Ops/sec |
Object | 67968.7 Ops/sec |
Let's break down the provided benchmark JSON and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The test compares four approaches for creating and updating objects:
object spread
)immutable-js
)native Map
)Object
)Each approach is measured using the same benchmark script, which iterates 100 times, adding a new key-value pair to an object.
Options Compared
Here's a brief overview of each option:
...
) to create a shallow copy of an existing object and then updates it with new key-value pairs.Map
object provided by JavaScript engines to create a data structure that's optimized for performance.obj[key] = {key, value};
) to update an existing object.Library Descriptions
Special JS Features
...
): Introduced in ECMAScript 2015, the spread operator allows creating a new object by spreading an existing object's properties into a new object.Alternative Approaches
For updating objects, other approaches include:
JSON.parse(JSON.stringify(obj))
with the replacer
option to perform deep cloning.Keep in mind that these alternative approaches may have different performance characteristics and trade-offs compared to the options being tested in this benchmark.