Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
assign vs spread test inside loop
(version: 1)
Comparing performance of:
assign vs spread
Created:
2 years ago
by:
Registered User
Jump to the latest result
Tests:
assign
const a = { title: 'asd' } for (let i = 0; i < 1000; i++) { Object.assign(a, {[i]: i}) }
spread
let a = { title: 'asd' }; for (let i = 0; i < 1000; i++) { a = { ...a, [i]: i } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
assign
spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
assign
490.4 Ops/sec
spread
2987.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **What is being tested?** The benchmark measures the performance difference between two approaches: 1. **`Object.assign()`**: This method copies properties from one or more source objects to a target object. 2. **Spread syntax (`...a, [i]: i`)**: This syntax creates a new object by copying all own enumerable properties from an existing object. **Options compared** The benchmark compares the performance of these two approaches inside a loop: * Loop iteration: 1000 times * Target object `a`: initialized with a single property `title` **Pros and Cons** 1. **`Object.assign()`**: * Pros: + Widely supported and well-maintained. + Can handle multiple source objects. * Cons: + May incur additional overhead due to method call and object creation. 2. **Spread syntax (`...a, [i]: i`)**: * Pros: + More concise and expressive. + Can be faster for simple assignments (e.g., assigning a single property). * Cons: + Less widely supported (older browsers) or may require polyfills. + Can lead to slower performance if used with complex objects or multiple properties. **Other considerations** * **Memory allocation**: Both approaches allocate memory for the target object and its properties. However, `Object.assign()` might allocate more memory due to additional overhead. * **Type coercion**: Spread syntax can lead to type coercion when assigning values to properties (e.g., `i` is a number, but `[i]` is a string). This might impact performance in certain scenarios. **Library and special JS feature** There are no specific libraries used in this benchmark. However, the spread syntax (`...a, [i]: i`) relies on JavaScript's built-in syntax for object spread. **Test user considerations** When running this benchmark, users should consider the following: * Older browsers may not support the spread syntax, which could impact performance and accuracy. * The benchmark measures the execution rate (executions per second), so users might want to run multiple iterations to get a more accurate result. **Alternatives** Other alternatives for handling object assignments in JavaScript include: 1. **`Object.create()`**: Creates a new object with the specified prototype chain. 2. **`Object.init()`**: Initializes an object with the given properties (less widely supported). 3. **Manual property assignment**: Assigning properties individually using `a[i] = i;`. These alternatives might offer different trade-offs in terms of performance, readability, and maintainability. If you have any further questions or need more context, feel free to ask!
Related benchmarks:
Which equals operator (== vs ===) is faster?
Which equals operator (== vs ===) is faster2?
Which equals operator (== vs ===) is faster? check for null
Compare equals operator ( == vs === )
Array.from vs Spread using 10000 elements
Comments
Confirm delete:
Do you really want to delete benchmark?