const firstObject = {}
const secondObject = {}
for(let i = 1; i < 1000; i++) firstObject[i] = Math.random();
for(let i = 1; i < 1000; i++) secondObject[i] = firstObject[i];
let rnd = Math.floor(Math.random() * 1000);
secondObject[rnd] = Math.random();
const finalObject = {
firstObject,
secondObject
};
console.log("first", firstObject)
console.log("second", secondObject)
console.log("final", finalObject)
const firstObject = {}
const secondObject = {}
for(let i = 1; i < 1000; i++) firstObject[i] = Math.random();
for(let i = 1; i < 1000; i++) secondObject[i] = firstObject[i];
let rnd = Math.floor(Math.random() * 1000);
secondObject[rnd] = Math.random();
const finalObject = Object.assign(firstObject, secondObject);
console.log("first", firstObject)
console.log("second", secondObject)
console.log("final", finalObject)
const firstObject = {}
const secondObject = {}
for(let i = 1; i < 1000; i++) firstObject[i] = Math.random();
for(let i = 1; i < 1000; i++) secondObject[i] = firstObject[i];
let rnd = Math.floor(Math.random() * 1000);
secondObject[rnd] = Math.random();
let diff = {}
diff[rnd] = secondObject[rnd];
const finalObject = Object.assign(firstObject, diff);
console.log("first", firstObject)
console.log("second", secondObject)
console.log("final", finalObject)
const firstObject = {}
const secondObject = {}
for(let i = 1; i < 1000; i++) firstObject[i] = Math.random();
for(let i = 1; i < 1000; i++) secondObject[i] = firstObject[i];
let rnd = Math.floor(Math.random() * 1000);
secondObject[rnd] = Math.random();
let diff = {}
diff[rnd] = secondObject[rnd];
const finalObject = {
firstObject,
diff
};
console.log("first", firstObject)
console.log("second", secondObject)
console.log("final", finalObject)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using the spread operator | |
Using Object.assign | |
Using Object.assign (known change) | |
Using the spread operator (known change) |
Test name | Executions per second |
---|---|
Using the spread operator | 2243.0 Ops/sec |
Using Object.assign | 3514.5 Ops/sec |
Using Object.assign (known change) | 7689.6 Ops/sec |
Using the spread operator (known change) | 7257.2 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark measures the performance of two approaches: using the spread operator (...
) to merge objects and using Object.assign()
to merge objects.
Test Cases
There are four test cases:
...
) to merge the original object with an object that has only the modified property set.Object.assign()
instead of the spread operator. It also includes a known change in the final object to make it harder for the browser to optimize.Object.assign()
to merge the original object with an object that has only the modified property set.Options Compared
The benchmark compares two options:
...
) to merge objectsObject.assign()
to merge objectsPros and Cons
Libraries and Special JS Features
There are no libraries or special JS features mentioned in the benchmark. However, it's worth noting that Object.assign()
is a built-in JavaScript method, so it doesn't require any external libraries.
Other Alternatives
If you need to merge objects, there are other alternatives besides using the spread operator and Object.assign()
. Some options include:
It's worth noting that the choice of approach depends on the specific use case and performance requirements. In general, using the spread operator is a good choice when working with large objects and modern browsers are optimized for it. However, Object.assign()
may still be a better option in certain scenarios, such as when working with older browsers or when security is a concern.