const firstObject = {
attempts: 1,
created_at: 1694778031091,
expires_at: 1694778091091,
source: {
externalId: "831f76fc-5a9d-49bc-8d4b-114010cedb73",
name: "Tengen Uzui",
type: "phone",
},
type: "phone",
value: "411273",
};
const finalObject = {
firstObject,
attempts: firstObject.attempts + 1,
};
const firstObject = {
attempts: 1,
created_at: 1694778031091,
expires_at: 1694778091091,
source: {
externalId: "831f76fc-5a9d-49bc-8d4b-114010cedb73",
name: "Tengen Uzui",
type: "phone",
},
type: "phone",
value: "411273",
};
const finalObject = Object.assign({
attempts: firstObject.attempts + 1,
}, firstObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using the spread operator | |
Using Object.assign |
Test name | Executions per second |
---|---|
Using the spread operator | 55731712.0 Ops/sec |
Using Object.assign | 8506396.0 Ops/sec |
Benchmark Explanation
The provided benchmark measures the performance of two approaches to modify an object in JavaScript:
...
): This method is used to create a new object by copying all own enumerable properties from one or more source objects into a new object.source
and assigns it to the object
. It also copies all enumerable own properties from source
to object
.Benchmark Definition
The benchmark definition in JSON format contains the following information:
Individual Test Cases
There are two test cases in the benchmark definition. Each test case contains:
"Using the spread operator"
).Options Compared
The two options compared in this benchmark are:
...
) to modify an object.Object.assign()
to modify an object.Pros and Cons of Each Approach:
...
):Object.assign()
for large datasets.Library Usage
There is no library usage in this benchmark. However, it's worth noting that Object.assign()
uses the V8 engine's built-in implementation of the method, which may provide better performance than a custom implementation.
Special JS Feature or Syntax
The spread operator (...
) was introduced in ECMAScript 2015 (ES6) as a new way to create objects and arrays. It provides a more concise and readable way to copy properties from one object to another.
Overall, this benchmark measures the performance of two different approaches to modifying objects in JavaScript, highlighting the trade-offs between conciseness, readability, and efficiency.