Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
8 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' } function newState(a, b) { return Object.assign({}, a, b); } const finalObject = newState(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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the details of this benchmark test. **Benchmark Description** The benchmark is testing the performance difference between two ways to merge two objects in JavaScript: using the spread operator (`...`) and using `Object.assign()`. **Test Case 1: Using the Spread Operator** In this test case, we have two objects: ```javascript const firstObject = { sampleData: 'Hello world' }; const secondObject = { moreData: 'foo bar' }; ``` The benchmark creates a new object by using the spread operator to merge these two objects: ```javascript const finalObject = { ...firstObject, ...secondObject }; ``` **Test Case 2: Using Object.assign()** In this test case, we have the same two objects as above. However, instead of using the spread operator, the benchmark defines a function `newState()` that takes two arguments (`a` and `b`) and returns a new object by merging them using `Object.assign()`: ```javascript function newState(a, b) { return Object.assign({}, a, b); } const finalObject = newState(firstObject, secondObject); ``` **Benchmark Results** The latest benchmark results show that the test running with `Object.assign()` performs at approximately 3624 executions per second on Firefox 60. In contrast, the test using the spread operator (`...`) runs at around 2426 executions per second. **Comparison of Options** Here's a comparison of the pros and cons of these two approaches: * **Spread Operator (`...`)**: + Pros: Concise syntax, easy to read and write. + Cons: May be slower than `Object.assign()` due to its dynamic nature (more overhead). * **Object.assign()**: + Pros: Generally faster than the spread operator, as it's a more efficient method for merging objects. + Cons: More verbose code, potentially harder to read. **Other Considerations** When choosing between these two methods, consider the following factors: 1. **Performance-critical code**: If you're working on performance-critical sections of your codebase, using `Object.assign()` might be a better choice due to its generally faster execution speed. 2. **Code readability and maintainability**: In cases where code readability and maintainability are more important than raw performance, the spread operator (`...`) is often a better option due to its concise syntax. **Library Used (None)** No external libraries or frameworks are used in this benchmark test. **Special JS Feature or Syntax (Spread Operator (`...`))** The benchmark uses the spread operator (`...`) as the primary way to merge objects in Test Case 1. The spread operator is a JavaScript feature that allows you to easily create new arrays or objects by spreading an existing array or object's elements into a new one. **Alternatives** If neither `Object.assign()` nor the spread operator are suitable for your needs, consider using other methods to merge objects, such as: 1. **Lodash's `_merge()`**: A more robust and feature-rich method for merging objects. 2. **Ramda's `merge()`**: Another popular library for functional programming in JavaScript. These alternatives may offer better performance or additional features depending on your specific use case.
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?