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:
6 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested on MeasureThat.net. The provided JSON benchmark measures the performance difference between using the JavaScript spread operator (`...`) and `Object.assign()` when merging two objects into a new object. **Options Compared:** 1. **Spread Operator (Rest Spreading):** The spread operator is used to merge one or more objects into another, spreading their properties onto it. 2. **Object.assign():** `Object.assign()` is a method that copies all enumerable own properties from one or more source objects to a target object. **Pros and Cons of Each Approach:** 1. **Spread Operator (Rest Spreading):** * Pros: + Can be more concise and readable, especially for simple cases. + Less error-prone than `Object.assign()`. + Works with non-object sources, such as arrays or primitive values. * Cons: + May not perform well with very large objects due to the overhead of creating new object literals. 2. **Object.assign():** * Pros: + Can handle large objects more efficiently than the spread operator. + More compatible with older browsers that do not support rest spreading. * Cons: + Can be less readable and verbose, especially for complex cases. **Library Used:** None explicitly mentioned in the provided JSON. However, `Object.assign()` is a built-in method of JavaScript objects, so no external library is required to use it. **Special JS Feature/Syntax:** The spread operator (rest spreading) was introduced in ECMAScript 2015 (ES6), which makes it a relatively new feature in JavaScript. It's supported by most modern browsers and engines, including Chrome 74. **Benchmark Preparation Code:** The provided benchmark preparation code sets up the test case objects `firstObject` and `secondObject`, and then creates a new object `finalObject` using either the spread operator or `Object.assign()`. **Other Alternatives:** 1. **Using `Object.merge()`:** Some libraries, like Lodash, provide an `Object.merge()` function that merges two or more objects into a new one. 2. **Using a library like Immer:** Immer is a library that provides a safe and efficient way to merge objects while minimizing the creation of temporary arrays. Keep in mind that the choice between these alternatives ultimately depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
JavaScript spread operator vs Object.assign performance for cloning
object assign vs object spread on growing objects
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?