Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance 1_2
(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', moreData: Math.random() } const secondObject = { sampleData: Math.random(), moreData: 'here we go2' } const finalObject = { ...firstObject, ...secondObject };
Using Object.assign
const firstObject = { sampleData: 'Hello world', moreData: Math.random() } const secondObject = { sampleData: Math.random(), moreData: 'here we go2' } 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using the spread operator
30676362.0 Ops/sec
Using Object.assign
37955352.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided JSON represents a benchmark test that compares the performance of two approaches to merge two objects in JavaScript: using the spread operator (`...`) and `Object.assign()`. **Test Case 1: Using the Spread Operator** In this test case, we have three objects: * `firstObject`: contains some sample data and a random value for `moreData`. * `secondObject`: also contains some sample data and a random value for `moreData`, but with different values than in `firstObject`. * `finalObject`: the merged result of `firstObject` and `secondObject`. The benchmark definition code is provided inline, which creates these objects and uses the spread operator (`...`) to merge them into `finalObject`. **Test Case 2: Using Object.assign** In this test case, we have the same three objects as in Test Case 1. However, instead of using the spread operator, we use the `Object.assign()` method to merge `firstObject` and `secondObject` into `finalObject`. **Options Compared:** * **Using the Spread Operator (`...`) vs. Using `Object.assign()`:** + Pros of Using the Spread Operator: - More concise and readable code. - Less memory allocation, as it creates a new array with references to the original objects' properties. + Cons of Using the Spread Operator: - May be slower due to the need for array creation and indexing. + Pros of Using `Object.assign()`: - Generally faster, as it uses native object merge functionality. + Cons of Using `Object.assign()`: - Less readable code, especially when dealing with complex objects or nested merges. - May allocate more memory due to the creation of a new array-like object. **Other Considerations:** * Both approaches have their use cases. The spread operator is often preferred for its conciseness and readability, while `Object.assign()` may be chosen when performance is critical, such as in server-side or high-performance client-side applications. * In modern JavaScript environments (ECMAScript 2018+), the spread operator is also more efficient due to optimized implementations by browsers like Chrome. **Library/Features Used:** None of the provided benchmark definitions use any libraries. However, if you're interested in exploring other options, `Object.assign()` was introduced as a method on the global object (`Object`) in ECMAScript 2015 (ES6). **Special JS Features/Syntax:** The spread operator (`...`) is a new feature introduced in ECMAScript 2018. It allows for concise object merging and is enabled by default in modern browsers. There you have it! The MeasureThat.net benchmark tests the performance of two approaches to merge objects in JavaScript, allowing users to compare the efficiency of these methods.
Related benchmarks:
object assign vs object spread on growing objects
JavaScript spread operator vs Object.assign performance (single addition)
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
JavaScript spread operator vs Object.assign performance test number 99
Comments
Confirm delete:
Do you really want to delete benchmark?