Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance ttt
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Using the spread operator
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = { ...firstObject, ...secondObject };
Using Object.assign
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = Object.assign({}, firstObject, secondObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using the spread operator
Using Object.assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using the spread operator
34097604.0 Ops/sec
Using Object.assign
9716053.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. The provided JSON represents two individual test cases, each with its own `Benchmark Definition`. These definitions describe how the JavaScript spread operator is compared to `Object.assign` in terms of performance. **Test Cases:** 1. **Using the spread operator**: This test case uses the syntax `{...firstObject, ...secondObject}` to create a new object that combines the properties of two existing objects, `firstObject` and `secondObject`. The spread operator (`...`) is used to merge these objects. 2. **Using Object.assign**: This test case uses the `Object.assign()` method to achieve the same result as the spread operator. It creates an empty object `{}` and then assigns properties from `firstObject` and `secondObject` using the spread operator. **Options Compared:** * Spread Operator (`{...firstObject, ...secondObject}`) * Object.assign() (`const finalObject = Object.assign({}, firstObject, secondObject);`) **Pros and Cons of Each Approach:** 1. **Spread Operator:** * Pros: + More concise and readable syntax + Does not require creating an empty object as a target + Often faster due to the language's internal optimization for merging objects * Cons: + May have slightly worse performance in older browsers or those with limited support for modern JavaScript features 2. **Object.assign()** * Pros: + More widely supported across different browsers and environments + Can be used for other purposes beyond just object merging (e.g., assigning values to a target object) * Cons: + Requires creating an empty object as a target, which can be unnecessary and less readable + May have slightly worse performance compared to the spread operator **Library/Utility Used:** None, this benchmark only relies on built-in JavaScript features. **Special JS Features/Syntax:** The test case uses modern JavaScript syntax, specifically the spread operator (`{...firstObject, ...secondObject}`). This syntax is supported in most modern browsers and Node.js versions, but may not work in older environments. **Other Alternatives:** If you needed to achieve object merging without using either the spread operator or `Object.assign()`, other alternatives might include: * Using a library like Lodash (`_.merge()` or `_.assign()`) * Implementing a custom merging function * Using a JSON merge strategy (e.g., `jsondiffpatch`) Keep in mind that these alternatives would likely have different performance characteristics and may not be as concise or readable as the spread operator or `Object.assign()`.
Related benchmarks:
parseInt vs Math.trunc
toFixed() vs Math.round().toString()
another test 2
parseInt vs Math.trunc 2
parseFloat(toFixed) vs Math.round()
Comments
Confirm delete:
Do you really want to delete benchmark?