function passFn(obj) {
console.log(obj);
}
const firstObject = {
id: 10737,
developer: 'СТРАНА Девелопмент',
residentialComplexName: 'asd',
residentialComplexLink: 'asd',
city: 'Москва',
station: '',
address: '',
releaseDate: '',
};
const secondObject = {
sumMin: '',
sumMax: '',
image: '',
roomsCount: '',
squareMeters: '',
apartSumMin: '',
apartSumMax: '',
regulationLink: 'asd',
presentationDeveloperLink: '',
presentationComplexLink: 'asd',
};
const finalObject = {
firstObject,
secondObject,
};
passFn(finalObject);
function passFn(obj) {
console.log(obj);
}
const firstObject = {
id: 10737,
developer: 'СТРАНА Девелопмент',
residentialComplexName: 'asd',
residentialComplexLink: 'asd',
city: 'Москва',
station: '',
address: '',
releaseDate: '',
};
const secondObject = {
sumMin: '',
sumMax: '',
image: '',
roomsCount: '',
squareMeters: '',
apartSumMin: '',
apartSumMax: '',
regulationLink: 'asd',
presentationDeveloperLink: '',
presentationComplexLink: 'asd',
};
passFn({ firstObject, secondObject });
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using the spread operator | |
Using spread to params |
Test name | Executions per second |
---|---|
Using the spread operator | 80689.8 Ops/sec |
Using spread to params | 173701.8 Ops/sec |
Let's break down what's being tested in this JavaScript microbenchmark.
Benchmark Definition
The benchmark is testing two approaches to merge two objects:
...
): This approach uses the spread syntax to merge two objects into a new object, like const finalObject = { ...firstObject, ...secondObject };
.passFn({ ...firstObject, secondObject });
.Options being compared
The benchmark is comparing the performance of these two approaches.
Pros and Cons
...
):Library and syntax
None mentioned in the provided benchmark definition. However, it's worth noting that JavaScript is a dynamically-typed language, which means you don't need explicit type definitions or library imports for most operations.
Special JS feature/syntax
No specific JavaScript features or syntax are highlighted in this benchmark, but using the spread operator is a relatively modern feature introduced in ECMAScript 2018 (ES2018).
Other alternatives
There are other ways to merge objects in JavaScript, such as:
Object.assign()
: This method takes one or more source objects and merges their properties into a target object.reduce()
method: This method can be used to create a new object by merging two objects.Example using Object.assign()
:
const finalObject = Object.assign({}, firstObject, secondObject);
Example using reduce()
:
const finalObject = Object.values(firstObject).reduce((acc, value, index) => {
acc[value] = secondObject[index];
return acc;
}, {});
However, these alternatives may not be as concise or readable as the spread operator approach.
Benchmark result
The benchmark results show that Chrome 103 is executing the Using spread to params
test case more frequently than the Using the spread operator
test case, with approximately 20 times more executions per second. This suggests that using the spread operator may be faster in this particular scenario.