Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs spread operator vs direct edit
(version: 0)
Comparing performance of:
Object.assign vs spread operator vs direct edit
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Object.assign
var params = { b:"hello", c: true, d:7 }; var other = Object.assign({ a: 2 }, params);
spread operator
var params = { b:"hello", c: true, d:7 }; var other = { a: 2, ...params };
direct edit
var params = { b:"hello", c: true, d:7 }; params.a = 2;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.assign
spread operator
direct edit
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 is being tested. **Benchmark Definition** The benchmark measures the performance of three ways to modify an object: using `Object.assign()`, the spread operator (`...`), and direct assignment. **Options Compared** * **Object.assign()**: This method takes two arguments: the target object and an optional source object. It merges the properties of the source object into the target object. * **Spread Operator (`...`)**: This syntax creates a new object by copying all own enumerable properties from one or more source objects. In this case, it's used to create a new object `other` with properties from another object `params`. * **Direct Assignment**: This method assigns a value directly to a property of an object. **Pros and Cons** * **Object.assign()**: Pros: allows for easy merging of objects, can handle nested objects. Cons: can be slower due to the overhead of function calls. * **Spread Operator (`...`)**: Pros: concise and readable syntax, can handle nested objects. Cons: may require more memory allocation than direct assignment. * **Direct Assignment**: Pros: simple and fast, no overhead of function calls or memory allocation. Cons: less readable and might not be suitable for complex merges. **Library and Special JS Features** In this benchmark, none of the options use a specific library. However, it's worth noting that `Object.assign()` was introduced in ECMAScript 2015 (ES6), while the spread operator is also part of ES6. **Benchmark Results** The latest benchmark results show the execution frequency per second for each option: * **Direct Edit**: 598795904 executions/second * **Object.assign()**: 2726845 executions/second (slower than direct edit) * **Spread Operator (`...`)**: 1297884 executions/second (faster than Object.assign(), but slower than direct edit) **Alternatives** If you're looking for alternatives to this benchmark, you could consider testing other methods of object modification, such as: * Using a library like Lodash or Underscore.js * Testing the performance of different data structures (e.g., arrays vs. objects) * Measuring the performance of complex object merges using different approaches Keep in mind that the results may vary depending on the specific use case and requirements.
Related benchmarks:
toFixed -> Number vs Math.round
toFixed() vs Math.round().toString()
toFixed() vs String(Math.floor()
toFixed() vs String(Math.floor()) vs Math.floor().toString()
toFixed vs Math.round() with numbers222
Comments
Confirm delete:
Do you really want to delete benchmark?