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)
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 = Object.entries(secondObject).filter((value, key) => key == 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 = Object.entries(secondObject).filter((value, key) => key == 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) | |
Using Object.assign (filtered) | |
Using the spread operator (filter) |
Test name | Executions per second |
---|---|
Using the spread operator | 2264.8 Ops/sec |
Using Object.assign | 2412.3 Ops/sec |
Using Object.assign (known change) | 6478.3 Ops/sec |
Using the spread operator (known change) | 6579.4 Ops/sec |
Using Object.assign (filtered) | 5364.5 Ops/sec |
Using the spread operator (filter) | 5213.0 Ops/sec |
Let's break down what's being tested in this benchmark:
Benchmark Context
The benchmark is comparing the performance of three ways to create a new object by merging two existing objects:
...
)Object.assign()
Object.assign()
with a filtered version of the target objectBenchmark Scenarios
The benchmark is run on different devices and browsers, including Chrome 126 on Linux desktops.
Performance Metrics
The performance metrics measured are:
ExecutionsPerSecond
: The number of executions per second for each test case.Test Cases
There are six test cases in total:
Object.assign()
to create a new object.Object.assign()
but only includes specific properties from the target object in the merge process.Object.assign()
.Results
The benchmark results show the performance of each test case across different devices and browsers. The results indicate that:
ExecutionsPerSecond
values.Object.assign()
with a filtered version (Test Case 3) performs poorly, with low ExecutionsPerSecond
values.Object.assign()
or the spread operator (Test Cases 5 and 6) perform similarly to each other.Overall, the benchmark suggests that using the spread operator with filtering is the most efficient way to create a new object by merging two existing objects.