const object1 = {
a: 1,
b: 2,
c: 3
};
const object2 = Object.assign({c: 4, d: 5}, object1);
const object1 = {
a: 1,
b: 2,
c: 3
};
const object2 = {c: 4, d: 5, object1};
const object2 = {c: 4, d: 5};
object2["a"] = 1;
object2["b"] = 2;
object2["c"] = 3;
const object2 = {c: 4, d: 5};
object2.a = 1;
object2.b = 2;
object2.c = 3;
const array1 = [ "hello", true, 7 ];
const array2 = [ 1, 2 ].concat(array1);
const array1 = [ "hello", true, 7 ];
[ 1, 2 ].push("hello", true, 7)
const array1 = [ 1, 2 ]
array1.push("hello")
array1.push(true)
array1.push(7)
const array1 = [ 1, 2 ]
const array2 = ["hello", true, 7, array1]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.assign | |
Object spread | |
Object Indexed Add | |
Object Dot Add | |
Array.prototype.concat | |
Array.prototype.push (grouped) | |
Array.prototype.push (single) | |
Array Spread |
Test name | Executions per second |
---|---|
Object.assign | 5172089.0 Ops/sec |
Object spread | 14043076.0 Ops/sec |
Object Indexed Add | 931925824.0 Ops/sec |
Object Dot Add | 916660992.0 Ops/sec |
Array.prototype.concat | 3414842.0 Ops/sec |
Array.prototype.push (grouped) | 49943964.0 Ops/sec |
Array.prototype.push (single) | 50199008.0 Ops/sec |
Array Spread | 38363776.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
The benchmark tests four different ways to perform object and array operations:
object2["a"] = 1;
).object2.a = 1;
).push()
method.{...object1}
).Object.assign()
.Now, let's discuss the pros and cons of each approach:
Object Indexed Add and Object Dot Add
Array.prototype.push (single) and Array.prototype.push (grouped)
push()
with multiple values can be slower due to the need for multiple function calls.Object spread
Object assign
Object.assign()
and object spread depends on the specific use case.In general, the performance differences between these methods are relatively small. However, when working with large datasets or performance-critical code, understanding these nuances can help optimize your code for better performance.
Keep in mind that these benchmarks may not reflect your specific use case, as browser optimizations and other factors can influence performance.