Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash merge vs lodash/fp merge vs custom merge vs spread
(version: 0)
Comparing performance of:
lodash merge vs lodash/fp merge vs custom merge vs spread
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
Script Preparation code:
var fp = _.noConflict(); function customMerge(a, b) { const result = {} for (const prop in a) if (a.hasOwnProperty(prop)) { result[prop] = b.hasOwnProperty(prop) ? a[prop].constructor === Object ? customMerge(a[prop], b[prop]) : b[prop] : a[prop] } return result }
Tests:
lodash merge
var a = { a: 'a', b: 'b', c: { a: 'ca', b: 'cb' }}; var b = { a: 'aa', c: {b: 'cbb'}}; var c = _.merge({}, a, b);
lodash/fp merge
var a = { a: 'a', b: 'b', c: { a: 'ca', b: 'cb' }}; var b = { a: 'aa', c: {b: 'cbb'}}; var c = fp.merge(b, a);
custom merge
var a = { a: 'a', b: 'b', c: { a: 'ca', b: 'cb' }}; var b = { a: 'aa', c: {b: 'cbb'}}; var c = customMerge(a, b)
spread
var a = { a: 'a', b: 'b', c: { a: 'ca', b: 'cb' }}; var b = { a: 'aa', c: {b: 'cbb'}}; var c = { ...a, ...b, c: { ...a.c, ...b.c, }, }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash merge
lodash/fp merge
custom merge
spread
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/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash merge
1262860.4 Ops/sec
lodash/fp merge
1017744.6 Ops/sec
custom merge
3581129.8 Ops/sec
spread
21919096.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, along with the pros and cons of each approach. **Benchmark Overview** The benchmark is comparing four different methods for merging objects: 1. Lodash `merge` function 2. Lodash functional programming (FP) `merge` function (`fp.merge`) 3. A custom merge function (`customMerge`) 4. Spread operator (`...`) **Options Compared** Each test case uses a similar input object structure: ```javascript { "a": "a", "b": "b", "c": { "a": "ca", "b": "cb" } } ``` The difference lies in the output object structure and how it's created. 1. Lodash `merge` function: Merges the input objects into a new object. ```javascript var c = _.merge({}, a, b); ``` 2. Lodash FP `merge` function: Similar to the first one, but uses functional programming principles. ```javascript var c = fp.merge(b, a); ``` 3. Custom merge function (`customMerge`): A hand-written implementation that recursively merges properties. ```javascript function customMerge(a, b) { // ... } var c = customMerge(a, b); ``` 4. Spread operator (`...`): Uses the spread operator to merge objects. ```javascript var c = { ...a, ...b, c: { ...a.c, ...b.c } }; ``` **Pros and Cons** Here's a brief summary of each approach: 1. **Lodash `merge` function**: * Pros: Well-tested, well-documented, and optimized for performance. * Cons: Adds an external dependency (Lodash). 2. **Lodash FP `merge` function**: * Pros: Similar to the first one, but with a more functional programming flavor. * Cons: Still adds an external dependency (Lodash). 3. **Custom merge function (`customMerge`)**: * Pros: No external dependencies, can be optimized for specific use cases. * Cons: Requires manual implementation and maintenance. 4. **Spread operator (`...`)**: * Pros: Lightweight, easy to implement, and widely supported. * Cons: Limited control over the merging process. **Library/Features Used** 1. Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, object merging, and more. 2. Lodash FP (`fp`) is a subset of Lodash specifically designed for functional programming principles. 3. The spread operator (`...`) is a built-in feature in modern JavaScript. **Special JS Features/Syntax** None mentioned explicitly in the benchmark. **Other Alternatives** If you're looking for alternatives to the spread operator, consider using `Object.assign()` or a library like Immer for state management and object updates. For more comprehensive object merging, explore libraries like Lodash or Ramda.
Related benchmarks:
lodash merge vs object.assign vs spread vs lodash assign
lodash merge vs test merge
lodash merge vs lodash/fp merge vs custom merge vs spread vs ramda merge
lodash merge vs custom merge js
Comments
Confirm delete:
Do you really want to delete benchmark?