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 | 24189.3 Ops/sec |
Spread two objects | 26174.3 Ops/sec |
Object.assign three objects | 10600.7 Ops/sec |
Spread three objects | 15264.8 Ops/sec |
Object.assign four objects | 6889.2 Ops/sec |
Spread four objects | 10860.3 Ops/sec |
Object.assign five objects | 5204.4 Ops/sec |
Spread five objects | 8977.7 Ops/sec |
Object.assign six objects | 4163.4 Ops/sec |
Spread six objects | 7041.8 Ops/sec |
Overview
The provided benchmark compares the performance of two JavaScript methods: Object.assign
and the spread operator (...
). The benchmark creates an array of 6 objects, populates each object with 100 random properties, and then uses each method to assign values to a target object.
Method 1: Object.assign
Object.assign
is a built-in method that assigns values from one or more source objects to a target object. In this benchmark, Object.assign
is used to copy the entire contents of an object into another object.
Method 2: Spread Operator (...
)
The spread operator is a new feature introduced in ECMAScript 2015 (ES6). It allows you to expand an array or an object into individual elements or properties. In this benchmark, the spread operator is used to create a shallow copy of an object and then assign values from it to another object.
Performance Comparison
The benchmark provides execution times for each test case, which can be used to compare the performance of Object.assign
and the spread operator. The results show that:
Object.assign
.Why is the Spread Operator Faster?
There are several reasons why the spread operator might be faster:
Object.assign
, which involves more complex operations like iterating over property names and values.Object.assign
, which can reduce overhead.Conclusion
In general, the spread operator is a better choice than Object.assign
when assigning values to individual properties or small sets of objects. However, for larger datasets or more complex assignments, Object.assign
might be a better option due to its flexibility and control over the assignment process.