<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 = new Map(obj);
obj.set(key, {key, value})
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
object spread | |
immutable-js | |
native Map |
Test name | Executions per second |
---|---|
object spread | 11239.3 Ops/sec |
immutable-js | 23490.1 Ops/sec |
native Map | 3176.2 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared, and analyzed.
Benchmark Overview
The test measures the performance of three approaches for creating and modifying objects:
...
operator)Map
constructor)The benchmark definition json provides a script preparation code that sets up an object (obj
) with 100 key-value pairs, where each value is another object with two properties: key
and value
. The script then updates the object by adding new key-value pairs using different methods.
Test Cases
There are three test cases:
...
operator to create a new object that includes all the properties of the original obj
, with each value being an object with key
and value
properties.Map
data structure, which is similar to JavaScript objects but immutable by default. The script creates a new Map
instance and adds key-value pairs using the set
method.Map
constructor to create a new map and add key-value pairs using the set
method.Comparison
The benchmark compares the performance of each approach across multiple executions per second for Firefox 121 on Desktop (Windows).
Pros and Cons of Each Approach:
Libraries and Special Features
...
operator to create a new object that includes all properties from an existing object.Other Alternatives
In conclusion, the benchmark measures the performance of three approaches for creating and modifying objects: object spread, Immutable.js set, and native JavaScript Map. Each approach has its pros and cons, and understanding these trade-offs is essential for optimizing performance-critical code in JavaScript applications.