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 < 100; 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 | 184918.9 Ops/sec |
Spread two objects | 41400.0 Ops/sec |
Object.assign three objects | 88899.8 Ops/sec |
Spread three objects | 21847.7 Ops/sec |
Object.assign four objects | 56413.3 Ops/sec |
Spread four objects | 14498.5 Ops/sec |
Object.assign five objects | 39123.2 Ops/sec |
Spread five objects | 10828.7 Ops/sec |
Object.assign six objects | 34141.0 Ops/sec |
Spread six objects | 8738.2 Ops/sec |
The benchmark titled "Spread vs Object.assign (mutation)" compares the performance of two different methods for merging JavaScript objects: using Object.assign
and using the spread operator (...
). It evaluates how both methods perform when merging a varying number of objects (from two to six), focusing on execution speed.
The two primary methods being compared in this benchmark are:
Object.assign
:
const object7 = Object.assign(object1, object2);
const object7 = Object.assign(object1, object2, object3);
Spread Operator (...
):
const object7 = {...object1, ...object2};
const object7 = {...object1, ...object2, ...object3};
Pros:
Cons:
...
)Pros:
Cons:
Object.assign
.The benchmark results show significant variances in execution speed between the two methods. The results reveal that:
Object.assign
outperforms the spread operator when merging two and three objects, but this trend shifts as more objects are included.Object.assign
.Readability and Maintainability: While the performance is important, the choice between Object.assign
and the spread operator should also consider code readability and maintainability. The spread operator is often seen as cleaner and more expressive.
Modern JavaScript Engines: The benchmark results can vary between different JavaScript engines (V8, SpiderMonkey, JavaScriptCore, etc.). Performance optimizations may change, affecting which method is faster over time.
Mutability and Side Effects: Given that Object.assign
mutates the target object, developers should be cautious when using it in contexts where immutability is desired.
In addition to the two methods compared in the benchmark, alternatives for merging objects include:
Lodash's _.merge()
: Lodash offers a robust utility library that includes deep merging of objects. However, it comes with a larger footprint and may not offer performance benefits compared to native approaches.
Using Object.entries()
and Array.prototype.reduce()
: One could merge objects using functional approaches, but these are often less performant for simple merging tasks compared to the tested methods.
This benchmark acts as a practical exploration of the performance characteristics of object merging techniques in JavaScript. It highlights the evolving standards and best practices in the language, as developers often need to balance performance with code clarity and functionality.