Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash merge vs object.assign vs spread overwriting one property
(version: 0)
Comparing performance of:
lodash merge vs object.assign vs spread
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Tests:
lodash merge
var a = { a: 'oh', b: 'my' }; var c = _.merge({a:true}, a);
object.assign
var a = { a: 'oh', b: 'my' }; var c = Object.assign({a:true}, a);
spread
var a = { a: 'oh', b: 'my' }; var c = { a:true, ...a };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash merge
object.assign
spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash merge
6155501.0 Ops/sec
object.assign
12627836.0 Ops/sec
spread
40911312.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark measures the performance of three different approaches to merge objects in JavaScript: `_.merge` from Lodash, `Object.assign`, and spread operator (`...`). The test creates two objects, `a` and `b`, with some common properties. Then, it defines a new object `c` using each of these methods. **Options Compared** The benchmark compares the performance of three approaches: 1. **_.merge` from Lodash**: This method merges two objects into one, overwriting any common properties. 2. **`Object.assign`**: This method copies all enumerable own properties from one or more source objects to a target object. 3. **Spread operator (`...`) with overwrite**: This method uses the spread operator to create a new object and then assigns the `a` object to it, overwriting any common properties. **Pros and Cons** * **_.merge` from Lodash:** + Pros: - Simple and concise syntax - Can handle nested objects + Cons: - May be slower than other approaches due to overhead of function call and argument validation * `Object.assign`:** + Pros: - Fast and efficient - Supports shallow copying of arrays and functions + Cons: - Less readable syntax for complex merge scenarios - Does not handle nested objects well * Spread operator (`...` with overwrite):** + Pros: - Modern and concise syntax - Can handle nested objects (in modern browsers that support it) + Cons: - May not work in older browsers or environments without object spread syntax - Can be less readable for complex merge scenarios **Library** The benchmark uses the Lodash library, which provides a `merge` function to merge two objects into one. This library is widely used and well-regarded in the JavaScript community. **Special JS Feature/Syntax** * The benchmark uses the spread operator (`...`) with overwrite syntax, which was introduced in ECMAScript 2018 (ES2018) as part of the Object Spread Operator (Spread Syntax) feature. This syntax allows you to create a new object and then assign an existing object to it using the spread operator. **Other Alternatives** If you're interested in exploring other alternatives for merging objects, here are some options: * **`Object.assign()` with nested objects**: You can use `Object.assign()` recursively by calling it on each property of the source object. * **Merges libraries like `merge-deep` or `lodash.merge Deep`**: These libraries provide more advanced merge functionality that handles nested objects and other edge cases. * **Custom implementation using `for...in` loop**: You can write a custom implementation using a `for...in` loop to iterate over the properties of both objects and assign values to the new object.
Related benchmarks:
lodash merge vs object.assign vs spread new obj
lodash.assign vs object.assign vs spread
lodash merge vs object.assign vs spread (no intermediate vars)
lodash assign vs object.assign vs spread operator - variable and constant
Comments
Confirm delete:
Do you really want to delete benchmark?