window.object1 = {};
window.object2 = {};
window.object3 = {};
window.object4 = {};
window.object5 = {};
window.object6 = {};
function makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
};
function populate(object) {
for (let i = 0; i < 10; i++) {
object[makeid()] = makeid();
}
};
populate(object1);
populate(object2);
populate(object3);
populate(object4);
populate(object5);
populate(object6);
const object7 = Object.assign({}, object1, object2);
const object7 = {object1, object2};
const object7 = Object.assign({}, object1, object2, object3);
const object7 = {object1, object2, object3};
const object7 = Object.assign({}, object1, object2, object3, object4);
const object7 = {object1, object2, object3, object4};
const object7 = Object.assign({}, object1, object2, object3, object4, object5);
const object7 = {object1, object2, object3, object4, object5};
const object7 = Object.assign({}, object1, object2, object3, object4, object5, object6);
const object7 = {object1, object2, object3, object4, object5, object6};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.assign two objects | |
Spread two objects | |
Object.assign three objects | |
Spread three objects | |
Object.assign four objects | |
Spread four objects | |
Object.assign five objects | |
Spread five objects | |
Object.assign six objects | |
Spread six objects |
Test name | Executions per second |
---|---|
Object.assign two objects | 262009.9 Ops/sec |
Spread two objects | 260144.5 Ops/sec |
Object.assign three objects | 179373.6 Ops/sec |
Spread three objects | 180740.7 Ops/sec |
Object.assign four objects | 134389.0 Ops/sec |
Spread four objects | 133780.3 Ops/sec |
Object.assign five objects | 105595.2 Ops/sec |
Spread five objects | 104432.9 Ops/sec |
Object.assign six objects | 85612.9 Ops/sec |
Spread six objects | 85289.5 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Definition
The benchmark is comparing the performance of two approaches:
Object.assign()
{...}
)Options Compared
Both approaches are used to merge objects, but with different syntax and behavior.
Object.assign()
assign()
method of an object to add properties from another object.Spread Syntax ({...}
)
{...}
) to create a new object with properties from another object.Object
, Array
).Object.assign()
.What's Being Tested
The benchmark measures the performance of both approaches in terms of:
Key Takeaways
{...}
) generally outperforms Object.assign()
in this benchmark.Overall, this benchmark provides a useful comparison between two commonly used approaches for merging objects in JavaScript.