Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance ( nested objects ) into empty object
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign into empty object
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Using the spread operator
const firstObject = { sampleData: 'Hello world', nested: { nestedData: "Worlds" } } const secondObject = { moreData: 'foo bar', oneNest: { secondNest: { nestedData: "Hellos" }} } const finalObject = { ...firstObject, ...secondObject };
Using Object.assign into empty object
const firstObject = { sampleData: 'Hello world', nested: { nestedData: "Worlds" } } const secondObject = { moreData: 'foo bar', oneNest: { secondNest: { nestedData: "Hellos" }} } const finalObject = Object.assign({}, firstObject, secondObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using the spread operator
Using Object.assign into empty object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using the spread operator
20791154.0 Ops/sec
Using Object.assign into empty object
8445812.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition** The provided JSON represents a benchmark test that compares the performance of two approaches for merging objects in JavaScript: 1. Using the spread operator (`...`): This approach uses the spread syntax to merge the properties of two objects into a new object. 2. Using `Object.assign()` method: This approach uses the `Object.assign()` method to merge the properties of two objects into a new object. **Options Compared** The benchmark test compares the performance of these two approaches in merging nested objects. The specific test cases use: * Two input objects: `firstObject` and `secondObject`, each with nested properties. * A final output object (`finalObject`) that is created by merging the two input objects using either approach. **Pros and Cons** **Using the Spread Operator (`...`)** Pros: * More concise and readable code * Can be used to merge objects in a more declarative way Cons: * May not be supported in older browsers or environments * Can lead to performance issues if the spread operator is not optimized (e.g., if it involves creating new arrays) **Using `Object.assign()` Method** Pros: * Widely supported across modern browsers and environments * Optimized for performance, as it uses a native method Cons: * Can be less readable or more verbose than the spread operator approach * May require additional setup or configuration to work correctly with nested objects **Library Used: None** There is no specific library used in this benchmark test. Both approaches rely on built-in JavaScript features and methods. **Special JS Feature/Syntax: Spread Operator (`...`)** The spread operator is a relatively modern feature introduced in ECMAScript 2015 (ES6). It allows for more concise object merging and array creation, but may not be supported in older browsers or environments. In this benchmark test, the spread operator approach is used to merge nested objects. **Other Considerations** When working with nested objects, it's essential to consider performance implications when choosing an approach. The spread operator can lead to performance issues if not optimized properly, while `Object.assign()` method is generally more reliable and optimized for performance. If you're writing a JavaScript application that needs to handle complex object merging or array creation, consider the trade-offs between conciseness, readability, and performance. **Alternatives** Some alternative approaches for merging objects in JavaScript include: * Using `lodash` library (e.g., `_merge()`) which provides a more concise and readable way to merge objects * Using a custom implementation with loops or recursive functions * Using other libraries like `immer` for immutable data structures Keep in mind that the choice of approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
object assign vs object spread on growing objects
JavaScript spread operator vs Object.assign performance (single addition)
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?