Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance 2
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
4 years ago
by:
Registered User
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 benchmark. **Benchmark Overview** The benchmark compares the performance of two approaches to merge two JavaScript objects: 1. Using the spread operator (`...`) 2. Using `Object.assign()` with an empty object as the target (`{}`) **Options Compared** * The spread operator (`...`) is a new way of merging objects introduced in ECMAScript 2018 (ES2018). It allows you to merge two or more objects into one using the syntax `object1 [...object2]`. * `Object.assign()` is an older method for merging objects, which was introduced in ECMAScript 2015 (ES6). **Pros and Cons of Each Approach** ### Spread Operator (`...`) Pros: * More concise and readable code * Can handle nested objects and arrays * Less prone to errors due to its more explicit syntax Cons: * Performance overhead compared to `Object.assign()` * May not be as widely supported in older browsers or environments ### Object.assign() Pros: * Wide support across browsers and environments * Simple and well-known API * Can handle nested objects and arrays, although with some limitations Cons: * More verbose code * Less readable for complex merges * May lead to errors due to its more implicit syntax **Library Usage** There is no library being used in this benchmark. **Special JavaScript Features or Syntax** The spread operator (`...`) uses a new feature of JavaScript that allows destructuring and spreading objects. This feature was introduced in ECMAScript 2018 (ES2018) and is widely supported across modern browsers and environments. **Other Alternatives** If the spread operator or `Object.assign()` were not available, other alternatives could be: * Using the dot notation (`obj1.prop = obj2.prop`) * Using a library like Lodash's `merge()` function * Manually concatenating arrays using `Array.prototype.push()` These alternatives would likely have different performance characteristics and trade-offs compared to the spread operator and `Object.assign()`. Overall, this benchmark is testing the performance of two modern JavaScript approaches for merging objects, which should provide insight into the efficiency and readability trade-offs of each method.
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?