Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Merge Arrays using lodash or not
(version: 0)
Merge Arrays using lodash or not
Comparing performance of:
merge1 vs merge2 vs merge3
Created:
2 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>
Script Preparation code:
function merge1(a, b) { return _.uniq(_.reject(_.concat(a, b), _.isUndefined)); } function emptyIfUndefined(a) { return a || []; } function merge2(a, b) { return _.uniq([...emptyIfUndefined(a), ...emptyIfUndefined(b)]); } function merge3(a, b) { return [...new Set([...emptyIfUndefined(a), ...emptyIfUndefined(b)])]; }
Tests:
merge1
merge1([1,2],[2,3]);
merge2
merge2([1,2],[2,3]);
merge3
merge3([1,2],[2,3]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
merge1
merge2
merge3
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/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
merge1
1115430.5 Ops/sec
merge2
2954314.5 Ops/sec
merge3
2639536.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and its components for you. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark that measures the performance of three different approaches to merge arrays: 1. Using Lodash's `uniq` function with `_.reject` to filter out undefined values (`merge1`) 2. Without using any library, filtering out undefined values first and then merging the arrays using `_.uniq` (not applicable due to missing library) However since its implementation is present in the script so merge2 is used which uses 'emptyIfUndefined' function 3. Using a simple Set data structure to remove duplicates (`merge3`) **Options Compared** The three options compared are: * `merge1`: Uses Lodash's `uniq` function with `_.reject` to filter out undefined values. + Pros: Efficiently removes undefined values and uses a well-known library for uniqueness checking. + Cons: Requires an external library (Lodash), which may add overhead due to HTTP requests. * `merge2`: Filters out undefined values first using the `emptyIfUndefined` function, then merges the arrays using Lodash's `uniq`. + Pros: Avoids requiring an external library by implementing filtering logic in-house, potentially reducing overhead. + Cons: Introduces additional complexity and may not be as efficient as a pure Lodash solution. * `merge3`: Uses a simple Set data structure to remove duplicates. + Pros: Simple and lightweight approach that doesn't require any external libraries. + Cons: May not be as efficient as the other two options, especially for larger arrays. **Libraries Used** The benchmark uses Lodash's JavaScript library (version 4.17.5). The `emptyIfUndefined` function is defined in-house to filter out undefined values from an array before merging it with another array. **Special JS Features/Syntax** None of the test cases use any special JavaScript features or syntax that would be relevant for a general audience. **Other Alternatives** For comparison, other approaches to merge arrays without using Lodash could include: * Using the `filter()` and `reduce()` methods of the Array prototype. * Implementing a custom filtering and merging logic using vanilla JavaScript. * Using a different library or framework that provides similar functionality (e.g., Ramda). However, since the benchmark is designed to compare the performance of three existing approaches, it's likely that these alternatives are not intended for direct comparison.
Related benchmarks:
compare _.merge() and lodash-merge
Lodash isUndefined vs isNil
merge vs mergeWith params
Lodash merge vs mergedeep 1
Comments
Confirm delete:
Do you really want to delete benchmark?