Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Objects spread vs object assign with large object
(version: 1)
Compare the new ES6 spread operator with the traditional Object.assign() method large object
Comparing performance of:
Object.assign vs spread operator
Created:
4 years ago
by:
Registered User
Jump to the latest result
Tests:
Object.assign
// Object.assign function createObject(fillValue) { return Object.fromEntries(new Array(1024*12).fill(fillValue).map((value, index) => { return [fillValue + index, fillValue + index]; })); } const params = createObject('params'); const other = Object.assign(createObject('assign'), params);
spread operator
// spread operator function createObject(fillValue) { return Object.fromEntries(new Array(1024*12).fill(fillValue).map((value, index) => { return [fillValue + index, fillValue + index]; })); } const params = createObject('params'); const other = {...createObject('spread'), ...params};
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.assign
spread operator
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 benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark compares two ways to create large objects in JavaScript: using the traditional `Object.assign()` method and the new ES6 spread operator (`...`). **Script Preparation Code** The script preparation code is not provided, but it's assumed that both test cases use the same underlying logic to generate a large object with 1024*12 properties. For the "spread operator" test case, the code uses `Object.fromEntries()` method, which is a part of the JavaScript standard library. This method creates an object from an array of key-value pairs. **Library Used:** In this case, the `Object.fromEntries()` method is used as a helper function to generate the large object. **Test Cases** There are two test cases: 1. **"Object.assign"**: This test case uses the traditional `Object.assign()` method to create the large object. 2. **"spread operator"**: This test case uses the new ES6 spread operator (`...`) to create the large object. **Pros and Cons of Each Approach** * **`Object.assign()`**: + Pros: widely supported, efficient in older browsers (before Chrome 100). + Cons: can be slower due to the method call overhead. * **Spread Operator (`...`)**: + Pros: concise, faster, and more modern syntax. + Cons: not supported in older browsers (before Chrome 100). **Other Considerations** * **Performance**: The spread operator is generally faster than `Object.assign()` due to its simpler syntax and lack of method call overhead. * **Readability**: Both approaches are relatively readable, but the spread operator provides a more concise way to create large objects. **Alternatives** If you need to support older browsers (before Chrome 100), you may consider using other libraries or polyfills to implement the spread operator. Some alternatives include: * Lodash's `_.assign()` method * A library like `object-assign` for compatibility with older browsers Keep in mind that if you're targeting modern browsers and want to take advantage of the spread operator, it's likely a better choice. For those interested in learning more about JavaScript features or syntax, some notable mentions include: * `Object.fromEntries()`: introduced in ECMAScript 2017. * `...` spread operator: introduced in ECMAScript 2015 (ES6). These features have improved performance and readability in modern JavaScript development.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
object assign vs object spread on growing objects
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?