Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance - new objects
(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 the JavaScript microbenchmark on MeasureThat.net. **What is tested:** The benchmark measures the performance difference between two methods of merging objects in JavaScript: 1. **Spread operator (`...`)**: This method uses the spread operator to create a new object by copying properties from an existing object. 2. **Object.assign()**: This method creates a shallow copy of one or more source objects, merging their properties into a target object. **Options compared:** The benchmark compares two options: 1. Using the spread operator (`...`) 2. Using Object.assign() **Pros and Cons:** * **Using the spread operator (`...`)**: + Pros: - More concise and readable code - Faster execution (in this benchmark, Chrome 117) + Cons: - Only works with objects that support spread syntax - May be slower for very large datasets due to object creation overhead * **Using Object.assign()**: + Pros: - Works with any JavaScript object, including arrays and functions - Can handle more complex merging scenarios (e.g., nested objects) + Cons: - More verbose code - May be slower due to the creation of a new object **Library usage:** None of the test cases use any external libraries. **Special JS features or syntax:** The benchmark uses the spread operator (`...`), which is a modern JavaScript feature introduced in ECMAScript 2018. It's not compatible with older browsers or versions of JavaScript that don't support it. **Other considerations:** * The benchmark measures the performance of creating new objects using both methods. * The test cases use simple object literals to represent the data being merged, which may not accurately reflect real-world scenarios where data is more complex. * The benchmark only runs on a desktop platform and Chrome 117 browser, which may limit its generalizability. **Alternatives:** * Other methods for merging objects in JavaScript include: + `Object.create()` + `Object.keys()` and `Object.assign()` + Library functions like Lodash's `merge()` or Ramda's `merge`
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?