Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniqBy performance lodash vs native
(version: 0)
lodash vs javascript
Comparing performance of:
lodash vs javascript vs native
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 sortBy = (key) => { return (a, b) => (a[key] > b[key]) ? 1 : (b[key] > a[key] ? -1 : 0); }; const uniqBy = (arr, a) => { arr.sort(sortBy(a)); const arr1 = []; arr1.push(arr[0]); for (let index = 1; index < arr.length; index++) { if (arr[index-1] && arr[index -1].a !== arr[index].a) { arr1.push(arr[index]); } } return arr1; } uniqBy(data, 'a')
native
function uniqueBy(arr, prop){ const record = [] const seen = {} for (let i = 0, len = arr.length; i < len; ++i) { // Notice the len = arr.length const item = arr[i] const val = item[prop] if (!seen[val]) { seen[val] = 1 record.push(item) } } } uniqueBy(data, 'a')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash
javascript
native
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
10302855.0 Ops/sec
javascript
6813315.5 Ops/sec
native
8606958.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark. **Overview** The benchmark is designed to compare the performance of three approaches to remove duplicates from an array: 1. **Lodash**: A popular JavaScript library that provides a `uniqBy` function for removing duplicates based on a specified property. 2. **Native JavaScript**: A custom implementation of the `uniqBy` function using only standard JavaScript features. 3. **Other alternative (not shown in this benchmark)**: An unknown approach that is not implemented here. **Options being compared** The main options being compared are: 1. **Lodash**: Uses the `lodash.core.js` library to provide the `uniqBy` function. 2. **Native JavaScript**: Implements the `uniqBy` function using standard JavaScript features, such as sorting and iterating over the array. **Pros and Cons of each approach** 1. **Lodash**: * Pros: Easy to use, well-tested, and widely adopted library. * Cons: Adds an external dependency (the library), which may slow down execution or cause issues with other libraries. 2. **Native JavaScript**: * Pros: No external dependencies, highly optimized for performance, and leverages the browser's built-in sorting algorithm. * Cons: Requires more manual effort to implement, and may not be as readable or maintainable as a library. **Library explanation** In this benchmark, `lodash` is used as a library to provide the `uniqBy` function. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array manipulation, and more. In this case, the `uniqBy` function is specifically designed to remove duplicates from an array based on a specified property. **Native JavaScript feature explanation** The custom implementation of `uniqBy` in Native JavaScript uses standard JavaScript features, such as: 1. **Sorting**: The `sort()` method is used to sort the array by the specified property. 2. **Iteration**: A loop is used to iterate over the sorted array and extract unique elements. **Other considerations** * The benchmark results show that the Native JavaScript implementation outperforms both Lodash and the custom JavaScript implementation. This suggests that, in this specific case, a native implementation can be more efficient than using an external library or implementing it manually. * The use of `len = arr.length` in the Native JavaScript implementation is interesting. This is because array lengths are not always accurately represented by the `length` property in older browsers. Using `arr.length` ensures that the loop iterates over the correct number of elements, even in cases where `length` might be out of date. **Alternatives** The benchmark only shows three alternatives: Lodash, Native JavaScript, and an unknown alternative (not implemented). There may be other approaches or libraries that could be used to implement `uniqBy`, such as: 1. **Underscore.js**: Another popular JavaScript utility library that provides a `uniq` function. 2. **Array.prototype.filter()`: Using the `filter()` method to remove duplicates from an array can also be effective, although it might not be as efficient as sorting and iterating over the array. Overall, this benchmark highlights the importance of considering performance and optimization when implementing algorithms or using external libraries in JavaScript applications.
Related benchmarks:
uniqBy performance
get uniq values js
uniqBy performance ttt
uniqBy performance and map
Comments
Confirm delete:
Do you really want to delete benchmark?