Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter unique, lodash vs Set vs filter()
(version: 0)
Comparing performance of:
Filter vs Set vs lodash
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
Script Preparation code:
function filterUniq(arr){ return arr.filter((i,p)=>{ return arr.indexOf(i) == p }); } function setUniq(arr){ return [...new Set(arr)] } var a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
Tests:
Filter
var b = filterUniq(a)
Set
var b = setUniq(a)
lodash
var b = _.uniq(a)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Filter
Set
lodash
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):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The benchmark in question is designed to compare three approaches for removing duplicates from an array: `filterUniq`, `setUniq`, and `_.uniq` (a function from the Lodash library). The test case uses a large array with repeated values, and each approach is executed repeatedly to measure their performance. **Approaches Compared** 1. **`filterUniq`**: This implementation uses the `indexOf` method to check if an element exists in the original array. If it does, it returns `false`, effectively filtering out duplicates. 2. **`setUniq`**: This approach uses the `Set` data structure to store unique elements. It converts the input array to a set and then back to an array using the spread operator (`...`). This method relies on JavaScript's built-in set implementation, which has an average time complexity of O(1) for insertion and lookup operations. 3. **`_.uniq` (Lodash)**: This function uses a similar approach to `filterUniq`, but with a more concise syntax. It iterates over the input array using `forEach`, checking each element against its index in the original array. **Pros and Cons of Each Approach** 1. **`filterUniq`**: * Pros: Simple, easy to understand. * Cons: Has a higher time complexity due to repeated `indexOf` calls (O(n^2) for large arrays). 2. **`setUniq`**: * Pros: Efficient use of JavaScript's built-in sets, making it suitable for large datasets. * Cons: Requires modern browsers or environments that support `Set`, and may not work as expected in older versions. 3. **`_.uniq` (Lodash)**: * Pros: Compact syntax, easy to read. * Cons: Has a slightly higher time complexity than `setUniq` due to the use of `forEach` (O(n)). **Library Overview** The Lodash library provides a collection of functional programming helpers, including `_.uniq`. Lodash is designed to be a utility belt for JavaScript developers, offering a range of functions for tasks like array manipulation, string manipulation, and more. In this benchmark, the `_.uniq` function serves as an alternative implementation for removing duplicates from an array. **Specialized JavaScript Feature or Syntax** The benchmark does not use any specialized JavaScript features or syntax beyond what's already discussed (e.g., `Set`, `forEach`). However, it's worth noting that modern JavaScript engines have introduced additional features like `Promise` and `async/await` which can affect performance in certain scenarios. These features are not explicitly used in the benchmark. **Other Alternatives** For removing duplicates from an array, other approaches could include: * Using a more efficient data structure like a `Map` or a `TreeSet`. * Implementing a custom algorithm using bitwise operations or other low-level techniques. * Utilizing specialized libraries or frameworks that provide optimized duplicate removal functionality (e.g., some sorting algorithms). These alternatives may have varying trade-offs in terms of performance, readability, and maintainability. The choice of approach depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
Array.prototype.filter vs Lodash filter (Even Numbers)
Array.prototype.filter vs Lodash without 2
Filter vs Set (unique elements)
Lodash.filter vs Lodash.without
Lodash.filter vs Lodash.without vs array.filter
Comments
Confirm delete:
Do you really want to delete benchmark?