Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq and sort vs sortedUniqBy
(version: 0)
Comparing performance of:
uniq .sort vs sortedUniqBy
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Script Preparation code:
window.productTypes = [ "TVProductKind.StreamingOnly", "", "p", "TVProductKind.Digitenne", "ProductCategory.TV", "TVProductKind.Digitenne", "TVProductKind.TVStreaming", "TVProductKind.TVStreaming", "TVProductKind.StreamingOnly", "TVProductKind.Digitenne", "ProductCategory.Internet", "TVProductKind.TVStreaming", "TVProductKind.ITV", "ProductCategory.Internet", "", "ProductCategory.Voice", "ProductCategory.TV", "TVProductKind.TVStreaming", "ProductCategory.TV", "ProductCategory.Internet", "TVProductKind.TVStreaming" ]; window.tvProductKinds = [ "TVProductKind.ITV", "TVProductKind.Digitenne", "TVProductKind.TVStrEaming", "TVProductKind.StreamingOnly" ]; window.priorityList = [ "ProductCategory.Internet", "ProductCategory.TV", "ProductCategory.Voice" ]; window.prioritarySorter = (a, b) => priorityList.indexOf(a) - priorityList.indexOf(b); window.reducedProductTypes = productTypes.reduce( (filtered, prod) => tvProductKinds.includes(prod) ? [...filtered, "ProductCategory.TV"] : priorityList.includes(prod) ? [...filtered, prod] : filtered, [] );
Tests:
uniq .sort
return _.uniq(reducedProductTypes.sort(prioritarySorter))
sortedUniqBy
return _.sortedUniqBy( reducedProductTypes, prioritarySorter );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
uniq .sort
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):
**Benchmark Overview** MeasureThat.net is a platform for creating and running JavaScript microbenchmarks, allowing users to compare the performance of different approaches. The benchmark in question compares two functions: `_.uniq` followed by `.sort` from the Lodash library, and `sortedUniqBy` also from Lodash. Both functions are used to sort an array of product types, removing duplicates while maintaining their original order. **Options Compared** 1. **Lodash _.uniq followed by .sort**: This approach uses the `_.uniq` function to remove duplicates from the array and then sorts the resulting array using the `.sort()` method. 2. **Lodash sortedUniqBy**: This approach directly uses the `sortedUniqBy` function, which is specifically designed for this purpose. It takes two arguments: the input array and a sorting function. **Pros and Cons** 1. **_.uniq followed by .sort**: * Pros: + Easy to understand and implement. + Can be used in cases where the original order of elements needs to be preserved. * Cons: + May incur additional overhead due to the extra function call and the `.sort()` method. 2. **sortedUniqBy**: + Pros: + Optimized for performance, as it is specifically designed for this purpose. + Reduces the number of function calls and iterations. * Cons: + May be less intuitive to understand and implement, especially for those without prior experience with Lodash. **Library: Lodash** Lodash is a popular JavaScript library that provides a collection of useful functions for common tasks. `_.uniq` removes duplicate elements from an array while preserving the original order, making it a convenient choice for this benchmark. The `sortedUniqBy` function is part of Lodash's set of utility functions and is designed to efficiently remove duplicates while maintaining the sorted order. It uses a combination of sorting and iteration to achieve optimal performance. **Special JS Feature/Syntax** This benchmark does not explicitly use any special JavaScript features or syntax, such as async/await, classes, or generators. However, it relies on modern JavaScript features like arrow functions (`=>`) and template literals (`\r\n`) for readability and convenience. **Other Alternatives** If you need to remove duplicates from an array while maintaining the original order, other approaches could be: 1. **Using a Set**: In modern browsers that support the `Set` data structure, you can use it to store unique elements and then convert it back to an array. 2. **Using a Map**: Similar to Sets, you can use Maps to store unique key-value pairs and then convert it to an array. However, these alternatives may not provide the same level of performance as Lodash's `sortedUniqBy` function or the `.uniq followed by .sort` approach. The final answer is that the choice between these approaches depends on your specific requirements, familiarity with JavaScript libraries, and performance considerations.
Related benchmarks:
_.sortBy vs native sort
Lodash isEqual with sort vs Lodash difference
isEqual + sort vs. difference
native sort of objects by linus
Lodash_4.17.21.sortBy vs .sort vs custom
Comments
Confirm delete:
Do you really want to delete benchmark?