Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance samba
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
2 years 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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 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
41180096.0 Ops/sec
Using Object.assign
10265569.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Definition JSON** The benchmark definition is not explicitly provided, but we can infer its purpose by looking at the individual test cases. The benchmark appears to be measuring the performance of two different methods for merging objects in JavaScript: 1. Using the spread operator (e.g., `const finalObject = { ...firstObject, ...secondObject }`) 2. Using `Object.assign()` (e.g., `const finalObject = Object.assign({}, firstObject, secondObject)`) **Test Cases** There are two individual test cases, each with its own benchmark definition and a unique name: 1. **Using the spread operator**: This test case uses the spread operator to merge two objects, `firstObject` and `secondObject`, into a new object, `finalObject`. 2. **Using Object.assign**: This test case uses the `Object.assign()` method to merge two objects, `firstObject` and `secondObject`, into a new object, `finalObject`. **Options Compared** The two options being compared are: * Using the spread operator (e.g., `{ ...firstObject, ...secondObject }`) * Using `Object.assign()` (e.g., `Object.assign({}, firstObject, secondObject)`) **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: **Using the Spread Operator:** Pros: * More concise and readable code * Easier to understand and maintain * Can be faster due to fewer function calls Cons: * May require more compiler optimizations or browser support * Can lead to memory leaks if not used carefully **Using Object.assign():** Pros: * Widely supported across browsers and Node.js versions * Familiar interface for developers who have used other languages with similar methods (e.g., Java's `Object.assign()`) Cons: * Less concise and less readable code compared to the spread operator * May be slower due to more function calls **Library Usage** None of the test cases explicitly use any libraries, but we can assume that the JavaScript engine being tested is a modern one that supports both the spread operator and `Object.assign()`. **Special JS Feature or Syntax** There doesn't seem to be any special JS feature or syntax used in these benchmark test cases. Both methods are standard and widely supported. **Other Alternatives** If you need to merge objects in JavaScript, there are a few other alternatives: * Using the `merge()` function from the Lodash library * Using the `Object.merge()` method (not widely supported) * Using the `assignIn()` method (a more concise version of `Object.assign()`) * Creating a custom merging function using object iteration and concatenation Keep in mind that performance differences between these methods may be small or negligible, depending on the specific use case and JavaScript engine being used.
Related benchmarks:
object assign vs object spread on growing objects
JavaScript spread operator vs Object.assign performance redux like
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
Spread Operator VS Object.assign performance analysis
Comments
Confirm delete:
Do you really want to delete benchmark?