Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance GifCo
(version: 0)
Perf test of Spread vs Object.assign()
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:
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):
**Benchmark Overview** The MeasureThat.net website allows users to create and run JavaScript microbenchmarks, including the provided benchmark: "JavaScript spread operator vs Object.assign performance GifCo". This benchmark compares the performance of two approaches to merge objects in JavaScript. **Options Compared** Two options are compared: 1. **Using the spread operator**: The `...` operator is used to merge two objects into a new object. Specifically, the test creates two objects, `firstObject` and `secondObject`, and then uses the spread operator (`...`) to merge them into a single object, `finalObject`. 2. **Using Object.assign():** The `Object.assign()` method is used to copy properties from one or more sources (in this case, `secondObject`) into a target object (`firstObject`). **Pros and Cons of Each Approach** 1. **Using the spread operator:** * Pros: + Can be more concise and readable than using Object.assign() + Can handle nested objects and arrays * Cons: + May not be supported in older browsers or environments that don't support the `...` operator 2. **Using Object.assign():** * Pros: + Widely supported across browsers and environments + Reliable way to merge objects, even with nested structures * Cons: + Can result in a more verbose code snippet **Library Used** None. **Special JS Feature or Syntax** The `...` operator used in the spread operator approach is a new JavaScript feature introduced in ECMAScript 2018 (ES10). This allows for concise merging of objects and arrays. If your target environment doesn't support this feature, you may need to use an alternative approach or polyfill. **Benchmark Results** According to the latest benchmark results, Chrome 109 on Windows Desktop achieved: * **Using Object.assign():** 4,892,538 executions per second * **Using the spread operator:** 1,764,053 executions per second These results suggest that using `Object.assign()` is generally faster than the spread operator approach. **Alternative Approaches** Other ways to merge objects in JavaScript include: * Using the `merge` function from a library like Lodash or Underscore.js * Implementing your own merge function using conditional statements and array manipulation * Using a template literal with nested properties (introduced in ECMAScript 2020) Keep in mind that each approach has its pros and cons, and the best choice depends on your specific use case, target environment, and performance requirements.
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 (single addition)
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
Comments
Confirm delete:
Do you really want to delete benchmark?