Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash merge vs object.assign vs spread
(version: 0)
Comparing performance of:
lodash merge vs object.assign vs spread
Created:
8 years ago
by:
Registered User
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 b = { c: 'goddess' }; var c = _.merge(a, b);
object.assign
var a = { a: 'oh', b: 'my' }; var b = { c: 'goddess' }; var c = Object.assign(a, b);
spread
var a = { a: 'oh', b: 'my' }; var b = { c: 'goddess' }; var c = { ...a, ...b };
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:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash merge
15844336.0 Ops/sec
object.assign
42837576.0 Ops/sec
spread
34650320.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the benchmark. **What is being tested?** MeasureThat.net is testing three different approaches for merging two objects in JavaScript: 1. `_.merge` from Lodash library 2. `Object.assign` 3. Spread syntax (`{ ...a, ...b }`) These approaches are being compared to determine which one is the most efficient. **Options comparison** Here's a brief overview of each option and their pros and cons: ### 1. `_` `.merge` from Lodash library Pros: * Well-maintained and widely used * Supports complex merge logic (e.g., merging arrays, objects with nested properties) Cons: * Requires an external dependency (Lodash) * May have slower startup times due to the dependency The `_.merge` function takes two objects as input and returns a new merged object. It also supports other data types like arrays. ### 2. `Object.assign` Pros: * Built-in JavaScript method, no additional dependencies required * Simple and straightforward implementation Cons: * Only works with objects, not with arrays or other data types * Can lead to shallow merges if used incorrectly (i.e., merging an object into another object without creating a new target) `Object.assign` is a built-in JavaScript function that takes multiple targets and an array of key-value pairs as arguments. When used with two objects, it returns a new merged object. ### 3. Spread syntax (`{ ...a, ...b }`) Pros: * Native JavaScript syntax, no additional dependencies required * Works with both objects and arrays Cons: * Introduced in ECMAScript 2018 (ES2018), might not be supported by older browsers or environments * Can lead to slower performance due to the creation of a new object The spread syntax allows you to create a new object by spreading an existing object's properties onto a target object. When used with two objects, it returns a new merged object. **Library and syntax features** Lodash is a popular utility library for JavaScript that provides various functions for tasks like string manipulation, array processing, and more. In this benchmark, `_.merge` is used to merge objects. The spread syntax (`{ ...a, ...b }`) is a modern JavaScript feature introduced in ECMAScript 2018 (ES2018). It allows you to create a new object by spreading an existing object's properties onto a target object. **Other alternatives** If these three approaches are not sufficient for your needs, here are some additional options: * `Object.create`: can be used to create a new object with the desired properties, but it requires more code and might lead to shallow merges if used incorrectly. * Custom merge functions: you can write your own custom function to merge objects, but this approach requires more code and expertise. Keep in mind that the best approach depends on your specific use case, performance requirements, and the environment where your code will run.
Related benchmarks:
lodash merge vs object.assign vs spread new obj
lodash.assign vs object.assign vs spread
Array Properties Merge: Lodash merge vs Object.assign
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?