Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash unionWith vs sortBy/sortedUniqBy
(version: 0)
Comparing performance of:
unionWith vs sortBy/sortedUniqBy
Created:
6 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
Script Preparation code:
var data1 = []; var data2 = []; for(i=0; i<100; i++){ data1.push({id: i, data: [1,2,3]}); data1.push({id: i + 25, data: [1,2,3]}); }
Tests:
unionWith
var result = _.unionWith(data2, data1, (d2, d1) => d2.id === d1.id);
sortBy/sortedUniqBy
var result = _.sortedUniqBy(_.sortBy([...data2, ...data1], (d) => d.id), (d) => d.id);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
unionWith
sortBy/sortedUniqBy
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 tests two different approaches to sorting and merging data: `lodash.unionWith` and `_.sortedUniqBy(_.sortBy([...data2, ...data1], (d) => d.id))`. Both functions are designed to work with arrays of objects and perform a specific operation. **Options compared** In this benchmark, we have two options being compared: 1. **`lodash.unionWith`**: This function merges two arrays (`data1` and `data2`) while removing duplicates based on the `id` property. It uses a custom comparison function to identify identical objects. 2. **`_.sortedUniqBy(_.sortBy([...data2, ...data1], (d) => d.id))`**: This approach first creates a new array by concatenating `data1` and `data2`, sorts the array by the `id` property using the `sortBy` function, removes duplicates using `sortedUniqBy`, and returns the resulting sorted array. **Pros and Cons** Here's a brief summary of each approach: * **`lodash.unionWith`**: + Pros: Efficiently merges arrays while removing duplicates. Customizable comparison function allows for fine-grained control. + Cons: May not perform well with very large datasets or complex data structures. * **`.sortedUniqBy(_.sortBy([...data2, ...data1], (d) => d.id))`**: + Pros: Can handle large datasets and complex data structures. Eliminates duplicates in a single pass. + Cons: Requires creating an intermediate sorted array, which may not be necessary for this specific use case. **Library usage** Both tests rely on the `lodash` library, specifically version 4.17.5. Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks like array manipulation, string processing, and more. **Special JS feature or syntax** There are no special features or syntax used in this benchmark. It's purely functional programming using the `lodash` library. **Other alternatives** If you're looking to implement similar functionality without relying on external libraries, here are some alternative approaches: 1. Use the `Array.prototype.reduce()` method to merge arrays and remove duplicates based on a custom comparison function. 2. Utilize the `Set` data structure to eliminate duplicates in an array while preserving the order of elements. Example code using `Array.prototype.reduce()`: ```javascript function unionWith(arr1, arr2, key) { const merged = arr1.slice(); for (const item of arr2) { if (!merged.find((d) => d[key] === item[key])) { merged.push(item); } } return merged; } // Usage: const data1 = [...]; // your array const data2 = [...]; // your array const result = unionWith(data1, data2, 'id'); ``` Using `Set` to eliminate duplicates: ```javascript function unionWith(arr1, arr2) { const merged = new Set(); for (const item of arr1) { merged.add(JSON.stringify(item)); } for (const item of arr2) { merged.add(JSON.stringify(item)); } return [...merged].map((item) => JSON.parse(item)); } // Usage: const data1 = [...]; // your array const data2 = [...]; // your array const result = unionWith(data1, data2); ``` Keep in mind that these alternatives may not offer the same level of performance or customization as using a dedicated library like Lodash.
Related benchmarks:
sortBy performance1
sortBy performance2
Sort lodash vs native
uniqBy performance lodash vs native
Comments
Confirm delete:
Do you really want to delete benchmark?