Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object spread vs. Object.assign
(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:
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's being tested in this JavaScript microbenchmark. **Benchmark Definition** The benchmark is comparing two approaches to merge objects: 1. **Object spread operator (Rest/spread syntax)**: `const finalObject = { ...firstObject, ...secondObject }` 2. **Object.assign()**: `const finalObject = Object.assign({}, firstObject, secondObject)` **What's being tested?** The benchmark is testing the performance difference between using the object spread operator and `Object.assign()` to merge two objects. **Options compared** Two options are being compared: 1. **Object spread operator (Rest/spread syntax)**: This approach uses the rest/spread syntax (`{ ... }`) to create a new object with properties from both `firstObject` and `secondObject`. 2. **Object.assign()**: This approach uses the `Object.assign()` method to merge two objects. **Pros and Cons of each approach** **Object spread operator (Rest/spread syntax)** Pros: * More concise and readable code * Can be used with functions as well (e.g., `Math.max(..., ...)`) Cons: * May have performance issues due to the creation of a new object * Not supported in older browsers (pre-ES6) **Object.assign()** Pros: * Well-supported across most browsers and versions * Allows for more control over the merged object Cons: * More verbose code compared to the spread operator * Can be less readable, especially when dealing with multiple objects **Other considerations** The benchmark also considers the library being used (in this case, none are specified), which means that any performance differences can be attributed solely to the JavaScript engine and its optimizations. There are no special JS features or syntax mentioned in this benchmark. Both approaches are standard and widely supported. **Alternative approaches** If you want to explore alternative approaches, here are a few: * Using destructuring assignment: `const { sampleData, moreData } = firstObject; const finalObject = { ...{ sampleData }, ...{ moreData } };` * Using the `reduce()` method: `const finalObject = Object.keys(firstObject).reduce((acc, key) => ({ ...acc, [key]: firstObject[key] }), {})` Keep in mind that these alternatives might not be as concise or readable as the original approaches, but they can provide interesting performance insights.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
object.assign vs spread to create a copy
object spread vs Object.assign
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?