Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread vs object assign array of objects
(version: 0)
Comparing performance of:
Assign vs Spread vs For for
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Assign
const a = { Color: 'red', Font: 'big' }; const colA = [ { Name: 'cola' }, { Name: 'colb' } ]; colA.forEach(item => { Object.assign(item, a); });
Spread
const a = { Color: 'red', Font: 'big' }; const colA = [ { Name: 'cola', ...a }, { Name: 'colb', ...a } ];
For for
const a = { Color: 'red', Font: 'big' }; const colA = [ { Name: 'cola' }, { Name: 'colb' } ]; for (let i = 0; i < colA.length; i++) { const item = colA[i]; for (const key in a) { item[key] = a[key]; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Assign
Spread
For for
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 their pros and cons. **Benchmark Overview** The benchmark tests three different ways to assign values to an array of objects in JavaScript: `Object.assign()`, spread operator (`...`), and a traditional `for` loop. The test cases are designed to measure the performance difference between these approaches. **Individual Test Cases** 1. **Assign** * Benchmark Definition: ```javascript const a = { Color: 'red', Font: 'big' }; const colA = [ { Name: 'cola' }, { Name: 'colb' } ]; colA.forEach(item => { Object.assign(item, a); }); ``` * This test case uses `Object.assign()` to assign the values of object `a` to each item in the array `colA`. * The pros of this approach are: + Easy to read and write. + Works with any type of object (arrays, objects, etc.). * The cons are: + Can be slower than other methods for large arrays. 2. **Spread** * Benchmark Definition: ```javascript const a = { Color: 'red', Font: 'big' }; const colA = [ { Name: 'cola', ...a }, { Name: 'colb', ...a } ]; ``` * This test case uses the spread operator (`...`) to assign the values of object `a` to each item in the array `colA`. * The pros of this approach are: + Can be faster than `Object.assign()` for large arrays. + More concise and readable. 3. **For Loop** * Benchmark Definition: ```javascript const a = { Color: 'red', Font: 'big' }; const colA = [ { Name: 'cola' }, { Name: 'colb' } ]; for (let i = 0; i < colA.length; i++) { const item = colA[i]; for (const key in a) { item[key] = a[key]; } } ``` * This test case uses a traditional `for` loop to assign the values of object `a` to each item in the array `colA`. * The pros of this approach are: + Can be more control over the assignment process. * The cons are: + More verbose and harder to read. **Library** There is no specific library used in this benchmark. The `Object.assign()` method is a built-in JavaScript function, while the spread operator (`...`) is also a built-in feature of the language. **Special JS Feature** The spread operator (`...`) is a relatively recent addition to JavaScript (introduced in ECMAScript 2018). It allows objects or arrays to be expanded into new properties or elements. **Benchmark Results** The latest benchmark results show that: 1. **For Loop**: The fastest execution per second, with an average of 2678577.25. 2. **Spread**: Faster than the `Object.assign()` method, with an average of 7350182.5 executions per second. 3. **Assign**: The slowest approach, with an average of 19460540.0 executions per second. **Other Alternatives** If you're looking for alternative ways to assign values to arrays of objects in JavaScript, some other options include: 1. `Array.prototype.map()`: This method returns a new array with the results of applying the provided function to each element. 2. `Array.prototype.forEach()` followed by a loop: Similar to the traditional `for` loop approach, but using an arrow function or a callback function. These alternatives may offer different trade-offs in terms of performance, readability, and conciseness.
Related benchmarks:
JavaScript spread operator vs Object.assign performance without mutating original object
JavaScript spread operator vs Object.assign performance create new
JavaScript spread operator vs Object.assign performance with {} target obj
JavaScript spread operator vs Object.assign performance (create new objects)
JavaScript spread operator vs Object.assign performance with new object in assign
Comments
Confirm delete:
Do you really want to delete benchmark?