Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread vs Object.assign vs assigning with Object.keys
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign vs Not merging vs For in
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Using the spread operator
const user = { firstName: 'One', lastName: 'Two', middleName: 'Three' } const defaultObject = { firstName: '', lastName: '', middleName: '' } const finalObject = { ...user, ...defaultObject };
Using Object.assign
const user = { firstName: 'One', lastName: 'Two', middleName: 'Three' } const defaultObject = { firstName: '', lastName: '', middleName: '' } const finalObject = Object.assign(defaultObject, user);
Not merging
const user = { firstName: 'One', lastName: 'Two', middleName: 'Three' } const defaultObject = { firstName: '', lastName: '', middleName: '' } Object.keys(defaultObject).forEach(key => {defaultObject[key] = user[key] })
For in
const user = { firstName: 'One', lastName: 'Two', middleName: 'Three' } const defaultObject = { firstName: '', lastName: '', middleName: '' } for(const property in defaultObject){ defaultObject[property] = user[property] }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Using the spread operator
Using Object.assign
Not merging
For in
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):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark, hosted on MeasureThat.net. The benchmark compares the performance of three different methods for merging objects: using the spread operator (`...`), `Object.assign()`, and iterating over object keys (`for...in`). **What is tested?** * **Using the spread operator**: This method creates a new object with the properties from two sources: an existing object (`user`) and another object (`defaultObject`). The spread operator (`...`) is used to merge these objects. * **Using `Object.assign()`**: This method directly merges the properties of two objects using the `assign()` method. It returns the new merged object. * **Not merging**: This test case does not attempt to merge the objects; instead, it simply copies all properties from one object (`user`) into another object (`defaultObject`). * **For-in**: This method iterates over an object's keys and assigns their values from another object (`user`) to corresponding keys in `defaultObject`. **Options compared** The benchmark compares these four methods: 1. Using the spread operator (`...`) 2. Using `Object.assign()` 3. Not merging 4. For-in iteration **Pros and Cons of each approach:** * **Using the spread operator (`...`)**: + Pros: concise, efficient way to merge objects. + Cons: may be slower for very large objects due to object creation overhead. * **Using `Object.assign()`**: + Pros: widely supported, easy to use. + Cons: may be slower than the spread operator due to method call overhead. * **Not merging**: + Pros: simple and fast, but does not achieve actual property assignment. (This option is likely included as a baseline for comparison.) + Cons: Not useful in most cases where property assignment is required. * **For-in iteration**: + Pros: flexible and can handle any object structure. + Cons: slower than spread operator or `Object.assign()` due to loop overhead. **Library usage** The benchmark uses the following library: * None explicitly. The benchmark relies on built-in JavaScript methods (`...`, `Object.assign()`, and `for...in`). **Special JS feature/syntax** The benchmark does not use any special JavaScript features or syntax beyond what is considered standard by modern JavaScript implementations. **Other alternatives** Alternative methods for merging objects in JavaScript include: * Using the `merge()` method from a library like Lodash. * Creating a new object and manually assigning properties using bracket notation (`obj[key] = user[key]`). * Using a custom implementation with more complex logic (e.g., handling cyclic references or nested objects). These alternatives may have different performance characteristics compared to the methods tested in this benchmark.
Related benchmarks:
object assign vs object spread on growing objects
object.assign vs spread to create a copy
object spread vs Object.assign
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
Comments
Confirm delete:
Do you really want to delete benchmark?