Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
assign comparison
(version: 1)
Comparing performance of:
spread vs assign vs spread new
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; var result = []; for (var i = 0; i < 1000; i++) { arr[i] = Math.random(); }
Tests:
spread
for (var i = 0, len = arr.length; i < len; i++) { const firstObject = { sampleData: 'Hello world', sampleData1: 'Hello world', sampleData2: 'Hello world', sampleData3: 'Hello world' }; result.push({...firstObject, isCloseTag: true}); }
assign
for (var i = 0, len = arr.length; i < len; i++) { const firstObject = { sampleData: 'Hello world', sampleData1: 'Hello world', sampleData2: 'Hello world', sampleData3: 'Hello world' }; result.push(Object.assign({firstObject, isCloseTag: true})); }
spread new
for (var i = 0, len = arr.length; i < len; i++) { const firstObject = { sampleData: 'Hello world', sampleData1: 'Hello world', sampleData2: 'Hello world', sampleData3: 'Hello world' }; const newObject = {...firstObject}; newObject.isCloseTag = true; result.push(newObject); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread
assign
spread new
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 JSON and explain what is tested, compared, and their pros and cons. **Benchmark Definition** The benchmark definition is a set of three test cases that compare different approaches to assign or spread properties to an object in JavaScript. 1. **Assign**: This approach uses the `Object.assign()` method to create a new object with the original object's properties and additional ones. 2. **Spread (New Object)**: This approach uses the spread operator (`...`) to create a new object with the original object's properties and additional ones. 3. **Assign vs Spread**: The third option is not explicitly defined, but it seems to be an alternative to the previous two approaches. **Options Comparison** * **Object.assign()**: + Pros: It is a widely supported method in older browsers and can handle cases where the target object has existing properties with the same name. + Cons: It can lead to unexpected behavior if the original object's properties have names that collide with the additional ones. * **Spread Operator (`...`)**: + Pros: It is a modern and efficient way to create new objects, and it avoids collisions by using dot notation for property names. + Cons: It may not be supported in older browsers or environments. **Other Considerations** * In all cases, the test creates an array of 1000 objects with random properties and assigns/ spreads them to a result array. This allows for a fair comparison of the performance and efficiency of each approach. * The `isCloseTag` property is added to the original object to introduce some variability in the testing. **Library Usage** There are no libraries explicitly mentioned in the benchmark definition, but it uses a few built-in JavaScript features: * **Object.assign()**: Part of the ECMAScript standard since ECMAScript 5 (2011) * **Spread Operator (`...`)**: Part of the ECMAScript standard since ECMAScript 2018 (2017) **Special JS Feature/Syntax** The spread operator (`...`) is a relatively recent addition to JavaScript, introduced in ECMAScript 2015 (ES6). It allows for more concise and expressive way to create new objects. Other alternatives that could be considered are: * Using `JSON.parse(JSON.stringify(obj))` to clone an object * Using a library like Lodash's `cloneDeep()` function to clone an object * Using a custom implementation of the assignment operator (`=`) or spread operator (`...`) However, these alternatives may not provide the same level of performance and efficiency as the built-in `Object.assign()` method or spread operator (`...`).
Related benchmarks:
array vs int16array try catch
Make array of primitives unique
Lodash 4.17.21 sort vs array.prototype.sort
Lodash sort vs array.prototype.sort - compare with taking ids from different array
Set.has v.s Array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?