Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance (corrected)
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
7 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 the provided benchmark and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Definition** The benchmark is comparing two ways to merge objects in JavaScript: using the spread operator (`...`) and `Object.assign()`. The goal is to determine which method is faster for a simple object merging task. **Options Compared** 1. **Using the spread operator**: This method uses the spread operator (`...`) to create a new object with the properties from both `firstObject` and `secondObject`. 2. **Using Object.assign()**: This method uses the `Object.assign()` function to create a new object by copying properties from two existing objects. **Pros and Cons** * **Using the spread operator**: + Pros: Easy to read and write, concise syntax. + Cons: Only supported in modern browsers (ECMAScript 2018+) and may not work in older browsers or environments that don't support the spread operator. * **Using Object.assign()**: + Pros: Widely supported across different browsers and environments, no dependencies required. + Cons: May require more code to achieve the same result (e.g., creating an empty object `{}` before assigning properties). **Library Used** None. This benchmark doesn't rely on any external libraries. **Special JS Feature or Syntax** The spread operator (`...`) is a new feature introduced in ECMAScript 2018+. If you're using an older browser or environment, this method might not work as expected. **Benchmark Preparation Code and Individual Test Cases** The provided JSON defines two test cases: 1. Using the spread operator: This code snippet demonstrates how to use the spread operator to merge `firstObject` and `secondObject`. 2. Using Object.assign(): This code snippet shows how to use `Object.assign()` to merge `firstObject` and `secondObject`. **Latest Benchmark Result** The latest benchmark results compare the execution times of both methods on a Chrome 67 browser running on a Windows desktop. * Using the spread operator: 1,810,151 executions per second. * Using Object.assign(): 1,223,895 executions per second. These results suggest that using the spread operator is slightly faster than using `Object.assign()` in this specific benchmark. However, it's essential to note that these results may vary depending on the browser, environment, and other factors. **Other Alternatives** If you need to merge objects in JavaScript, there are a few alternative approaches: * Using the `{...}` syntax with an empty object: `const finalObject = { ...{}, ...firstObject, ...secondObject };` * Using `Array.prototype.reduce()`: `finalObject = firstObject.reduce((acc, current) => ({ ...acc, ...current }), secondObject)` * Using a library like Lodash's `merge()` function * Manual property assignment: `const finalObject = {}; finalObject.sampleData = firstObject.sampleData; finalObject.moreData = secondObject.moreData;` Keep in mind that each of these alternatives has its own trade-offs and may not be suitable for all use cases.
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?