<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>
const obj = {};
for(i=0;i<100000;i++){
obj[i] = 'some long string which will need to be copied';
}
const obj2 = {key: 'This is final object'}
const final = {obj2, obj};
const obj = {};
for(i=0;i<100000;i++){
obj[i] = 'some long string which will need to be copied';
}
const obj2 = {key: 'This is final object'}
const final = Object.assign({}, obj2, obj)
const obj = {};
for(i=0;i<100000;i++){
obj[i] = 'some long string which will need to be copied';
}
const immObj = Immutable.Map();
const obj2 = {key: 'This is final object'}
const final = immObj.set(obj).set(obj2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
object spread | |
object assign | |
immutable-js |
Test name | Executions per second |
---|---|
object spread | 33.5 Ops/sec |
object assign | 29.2 Ops/sec |
immutable-js | 434.6 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmarking test, specifically designed to compare the performance of three different approaches for object assignment:
...
operator)Object.assign()
method)Immutable.Map().set()
method)Benchmark Preparation Code
The script preparation code includes two external libraries:
The benchmark preparation code is minimal and only sets up the necessary environment for the test cases to run. It defines an empty object obj
and another object obj2
, which will be used as input data for each test case.
Individual Test Cases
Each test case has a unique name and a benchmark definition that describes how the assignment should be performed:
...
) to merge two objects: final
and obj
.Object.assign()
method to merge two objects: final
and obj
.Immutable.Map().set()
method, which creates a new immutable map and sets two properties on it.Comparison of Options
Here's a brief overview of each approach:
Special JS Features
There are no special JavaScript features or syntax used in this benchmark. The focus is on the assignment methods themselves.
Other Alternatives
If you're looking for alternative methods, consider:
push()
method can be faster than object assign.add()
method can be faster than object assign.Keep in mind that these alternatives may not be suitable for all use cases and might require additional setup or configuration.