Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniqBy performance
(version: 0)
lodash vs javascript
Comparing performance of:
lodash vs javascript
Created:
6 years ago
by:
Registered User
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')
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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0
Browser/OS:
Firefox 120 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
1777446.1 Ops/sec
javascript
2259416.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of two approaches to create an array with unique elements, sorted by a specific attribute. **Script Preparation Code and Options Compared** The script preparation code for this benchmark is a JavaScript array `data` that contains 9 elements, including duplicates. The array has an attribute `a` that can be used as a key for sorting or filtering the elements. Two options are compared: 1. **Lodash (`_.uniqBy(data, 'a')`)**: This approach uses the `uniqBy` function from the Lodash library, which removes duplicate elements based on a specified attribute. The function sorts the array first and then iterates through it to filter out duplicates. 2. **Custom JavaScript implementation (`const uniqBy = (arr, a) => { ... }; uniqBy(data, 'a')`)**: This approach implements a custom `uniqBy` function that uses the `sort()` method to sort the array by the specified attribute and then iterates through it to filter out duplicates. **Pros and Cons of Each Approach** 1. **Lodash (`_.uniqBy(data, 'a')`)**: * Pros: Fast and efficient, as Lodash is a well-optimized library. * Cons: Requires an additional external library (Lodash) to be included in the project, which can increase bundle size. 2. **Custom JavaScript implementation (`const uniqBy = (arr, a) => { ... }; uniqBy(data, 'a')`)**: * Pros: No external dependencies required, and can be optimized for specific use cases. * Cons: Requires more manual effort to implement the logic correctly and maintainably. **Library Used** The Lodash library is used in this benchmark. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, string manipulation, and data processing. **Special JS Feature or Syntax** None are explicitly mentioned in the provided code snippet. However, it's worth noting that some modern JavaScript features like `const` and arrow functions (`=>`) are used in the custom implementation. **Other Alternatives** If you prefer not to use Lodash or implement a custom solution, other alternatives for creating an array with unique elements include: 1. **Set**: You can convert the array to a Set data structure, which automatically removes duplicates. Then, iterate through the Set and create a new array with only the unique elements. 2. **Array.prototype.reduce()**: Similar to using Set, you can use `reduce()` method to iterate through the array and collect unique elements. Example code: ```javascript const uniqBySet = (arr, a) => [...new Set(arr.map(item => item[a]))].map(item => arr.find(i => i[a] === item)); ``` Keep in mind that these alternatives might have slightly different performance characteristics compared to Lodash or the custom implementation.
Related benchmarks:
uniqBy vs stringify performance
uniqBy performance ttt
uniqBy performance lodash vs native
uniqBy performance and map
Comments
Confirm delete:
Do you really want to delete benchmark?