Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs set vs uniqSort
(version: 0)
Comparing performance of:
Set then sort vs uniq then sort vs sort then sortedUniq
Created:
4 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>
Tests:
Set then sort
var l = new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]); return [...new Set(l)].sort();
uniq then sort
var l = ["A", "B", "C", "C", "A", "F", "Z", "M", "M", "M", "M", "P", "R", "Q", "A", "F"]; return _.uniq(l).sort();
sort then sortedUniq
var l = ["A", "B", "C", "C", "A", "F", "Z", "M", "M", "M", "M", "P", "R", "Q", "A", "F"]; return _.sortedUniq(l.sort());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set then sort
uniq then sort
sort then sortedUniq
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set then sort
724893.2 Ops/sec
uniq then sort
747303.0 Ops/sec
sort then sortedUniq
487868.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmark. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches: 1. Using a `Set` object, followed by sorting the set (referred to as "Set then sort"). 2. Using the `_uniq` function from the Lodash library, which removes duplicates from an array, followed by sorting the resulting array (referred to as "uniq then sort"). 3. Using the `_sortedUniq` function from the Lodash library, which sorts and removes duplicates in a single step (referred to as "sort then sortedUniq"). **Set vs Uniq** The "Set then sort" approach uses a `Set` object to remove duplicates from an array, followed by sorting the set. Here's why this might be considered: Pros: * Simple and straightforward implementation. * Good for edge cases where you need to remove duplicates before sorting. Cons: * Creating a `Set` object involves iterating over the entire array, which can be slower than other methods. * Sorting a `Set` is not as efficient as sorting an array, since `Set`s are typically implemented as hash tables and don't have a built-in sorting algorithm. **Uniq vs Sorted Uniq** The "_uniq then sort" approach uses the `_uniq` function from Lodash to remove duplicates from an array, followed by sorting the resulting array. Here's why this might be considered: Pros: * Lodash provides optimized implementation for removing duplicates and sorting. * This approach avoids creating a `Set` object, which can reduce memory usage. Cons: * The `_uniq` function may have additional overhead compared to using a simple `Set`. * Sorting the resulting array from `_uniq` might not be as efficient as sorting an array directly. **Sorted Uniq** The "sort then sortedUniq" approach uses the `_sortedUniq` function from Lodash, which sorts and removes duplicates in a single step. Here's why this might be considered: Pros: * Optimized implementation that combines two operations into one. * Avoids the overhead of creating a `Set` object or sorting an array separately. Cons: * May have additional complexity compared to simpler approaches like "Set then sort". * Requires the use of Lodash, which may add extra dependencies. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, string processing, and more. The `_uniq` and `_sortedUniq` functions are part of this library, providing optimized implementations for removing duplicates and sorting arrays. In the benchmark, using Lodash's `_sortedUniq` function takes advantage of its optimized implementation to combine two operations into one, potentially improving performance compared to simpler approaches. **Other Alternatives** If you don't have access to Lodash or prefer not to use it, alternative approaches could include: * Using a simple `Set` object and sorting it manually. * Implementing your own `_uniq` function using iteration over the array. * Using other libraries that provide similar functionality to Lodash. Keep in mind that each of these alternatives may have trade-offs in terms of performance, complexity, or dependencies.
Related benchmarks:
lodash uniq vs native uniq
lodash uniqBy vs custom uniqBy
uniqBy performance lodash vs native
uniqBy performance and map
Lodash uniqBy vs Javascript uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?