Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash merge vs lodash assign vs object.assign vs spread 2
(version: 0)
Comparing performance of:
lodash merge vs lodash assign vs spread vs assign
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js'></script>
Tests:
lodash merge
var a = { a: 'oh', b: 'my', d: { x: 1 } }; var b = { c: 'goddess', d: { x: 2, y: 99 } }; var c = _.merge(a, b);
lodash assign
var a = { a: 'oh', b: 'my', d: { x: 1 } }; var b = { c: 'goddess', d: { x: 2, y: 99 } }; var c = _.assign(a, b);
spread
var a = { a: 'oh', b: 'my', d: { x: 1 } }; var b = { c: 'goddess', d: { x: 2, y: 99 } }; var c = { ...a, ...b };
assign
var a = { a: 'oh', b: 'my', d: { x: 1 } }; var b = { c: 'goddess', d: { x: 2, y: 99 } }; var c = Object.assign({}, a, b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash merge
lodash assign
spread
assign
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's being tested. **What is being tested?** The benchmark is comparing the performance of four different ways to merge two objects in JavaScript: 1. `_.merge` from Lodash 2. `_.assign` from Lodash 3. Using the spread operator (`{ ...a, ...b }`) 4. Using `Object.assign()` with an empty object as the first argument (`Object.assign({}, a, b)`) **Options being compared:** * **Lodash merge (`_.merge`) and Lodash assign (`_.assign`)**: These two functions are part of the Lodash library, which provides a set of useful JavaScript utilities. Both functions take two objects as input and return a new object that is the result of merging or assigning properties from both objects. + Pros: - Easy to use and understand - Provide additional features beyond simple object assignment (e.g., support for arrays, null/undefined values) + Cons: - Add dependencies (the Lodash library) - May have a larger overhead compared to native JavaScript methods * **Spread operator (`{ ...a, ...b }`)**: This syntax is a shorthand way to create a new object with properties from both `a` and `b`. + Pros: - Very concise and easy to read - No dependencies or additional overhead + Cons: - Only works for objects with the same structure (i.e., same keys) - Can be less readable if the objects have different structures * **`Object.assign()` with an empty object as the first argument (`Object.assign({}, a, b)`)**: This method is used to merge two or more objects into one. + Pros: - Works for any type of objects (not just plain JavaScript objects) - Can be used with other types of values (e.g., arrays, null/undefined) if needed + Cons: - May have a larger overhead compared to native JavaScript methods - Requires creating an empty object first **Other considerations:** * The benchmark uses a simple example where two objects are merged into one. In real-world scenarios, you may need to handle more complex cases (e.g., merging arrays, handling null/undefined values). * The use of the Lodash library adds dependencies and overhead, which can impact performance. * The spread operator is a concise way to merge objects but may not be suitable for all cases. **Latest benchmark result:** The latest results show that: * `Object.assign()` with an empty object as the first argument performs the best ( lowest executions per second) * `_.merge` from Lodash comes in second * `_.assign` from Lodash is third * Using the spread operator performs the worst This suggests that for simple object merging, using native JavaScript methods (`Object.assign()`) or Lodash functions (`_.merge` and `_._assign`) may be more efficient than relying on the spread operator.
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 overwriting one property
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?