<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 = obj.set(key, {key, value})
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
object spread | |
immutable-js | |
es6 |
Test name | Executions per second |
---|---|
object spread | 4518.0 Ops/sec |
immutable-js | 46373.5 Ops/sec |
es6 | 126672.9 Ops/sec |
Let's dive into the explanation.
Benchmark Overview
The provided benchmark compares the performance of three different approaches for adding elements to an object: object spread
(using JavaScript's spread operator), immutable-js set
, and es6 Map.set()
method. The benchmark is designed to measure the performance of each approach by creating a large object and repeatedly adding new key-value pairs to it.
Options Compared
{...obj, [key]: {key, value}}
) to add new properties to an existing object.set()
method for creating immutable sets, which can be used to create an object-like data structure. In this benchmark, the immutable-js set
approach uses obj.set(key, {key, value})
to add elements to the object.Map.set()
method (introduced in ECMAScript 2015) to add new key-value pairs to a Map
object.Pros and Cons of Each Approach
Library Descriptions
set()
method creates an immutable set, which can be used to create an object-like data structure.set()
function for creating immutable sets.Special JS Features
In this benchmark, no special JavaScript features or syntax are used beyond what is required by the benchmark definition (ES6 Map.set() method). However, it's worth noting that modern browsers support many advanced JavaScript features and syntax, such as async/await, classes, and arrow functions.