<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 r = final.key
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);
const r = final.get("key")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
object spread | |
object assign | |
immutable-js |
Test name | Executions per second |
---|---|
object spread | 29.3 Ops/sec |
object assign | 30.3 Ops/sec |
immutable-js | 337.4 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net, which compares the performance of different approaches for creating a new object with data from an existing object.
Approaches Compared
There are three approaches compared:
{...obj}
) to create a new object and copy data from obj
.Object.assign()
to merge two objects.Pros and Cons of Each Approach
Library Descriptions
Special JS Features/Syntax
None mentioned explicitly in the provided benchmark definitions, but it's worth noting that the spread operator ({...obj}
) is a relatively recent feature (introduced in ES6) and may not be supported by older browsers or JavaScript engines.
Other Alternatives
If you're interested in exploring other approaches or alternatives, here are a few options:
_.merge()
and _.assign()
.Keep in mind that the choice of approach often depends on the specific requirements and constraints of your project.