Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniqBy vs custom uniqBy
(version: 0)
the custom uniqBy doesn't rely on sorting. Instead it creates an object with the already added values
Comparing performance of:
lodash vs javascript
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script>https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.core.js</script>
Script Preparation code:
var data = [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}, {a: 6}, {a: 7}, {a: 8}, {a: 1}];
Tests:
lodash
_.uniqBy(data, 'a');
javascript
const uniqueBy = (array, fieldName) => { const uniqueArr = []; const addedFields = {}; array.forEach((el) => { if (addedFields[fieldName]) { return; } addedFields[el[fieldName]] = true; uniqueArr.push(el); }) return uniqueArr; } uniqueBy(data, 'a')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
javascript
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):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net, which compares two approaches to remove duplicate entries from an array: using Lodash's `uniqBy` function and a custom implementation. **Library Used: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks like data manipulation, string manipulation, and more. In this case, the `uniqBy` function is used to remove duplicate entries from an array based on a specified field value. **Custom Implementation** The custom implementation uses a simple algorithm to achieve the same result as Lodash's `uniqBy`. The main difference between the two approaches is that Lodash's `uniqBy` sorts the array before removing duplicates, whereas the custom implementation creates an object with already added values to keep track of unique entries. **Options Compared** The benchmark compares two options: 1. **Lodash's `uniqBy` function**: This approach relies on sorting the array and uses a stable sort algorithm (e.g., merge sort) to preserve the original order of equal elements. 2. **Custom implementation**: This approach creates an object with already added values to keep track of unique entries, avoiding sorting altogether. **Pros and Cons** **Lodash's `uniqBy` function:** Pros: * Faster execution times for large datasets (due to optimized sorting algorithms) * Easier to implement and maintain, as it provides a standardized function with minimal code changes Cons: * Sorting the array can lead to slower performance if the dataset is already sorted or has many duplicate entries * May not be suitable for real-time applications where data is constantly changing **Custom implementation:** Pros: * Faster execution times for small to medium-sized datasets (due to avoiding unnecessary sorting) * Suitable for real-time applications where data is constantly changing Cons: * More complex implementation, requiring additional code and logic to manage the object * May not be as efficient for large datasets due to the need to iterate over the entire array **Device and Browser Variations** The benchmark results show variations in execution times across different devices and browsers. These variations can be attributed to factors like: * Different CPU architectures and speeds * Memory constraints (e.g., mobile devices vs. desktops) * Browser-specific optimizations or overheads **Special JavaScript Features/ Syntax** This benchmark does not use any special JavaScript features or syntax that would require specific explanations. It focuses on the core implementation details of the `uniqBy` function and custom implementation. **Alternative Approaches** Other approaches to remove duplicate entries from an array include: * Using a Set data structure (e.g., with `Set()` and `Array.from()`) * Utilizing other libraries like `moment.js` or `uuid` * Implementing a manual loop-based approach using arrays and hash tables Each of these alternatives has its own trade-offs in terms of performance, complexity, and maintainability.
Related benchmarks:
uniqBy performance
uniqBy performance ttt
uniqBy performance lodash vs native
uniqBy performance and map
Comments
Confirm delete:
Do you really want to delete benchmark?