Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash/uniq vs filter+set vs array→set→array
(version: 0)
Comparing performance of:
lodash/uniq vs filter+set vs array→set→array
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var smalldata = [1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok']; var bigdata = [1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok',1,2,3,4,5,'ok','abc','def',null,undefined,1,2,3,'ok']; var uniq = (arr) => { var dups = new Set(); return arr.filter((v) => { if (dups.has(v)) { return false; } dups.add(v); return true; }); }; var uniqset = (arr) => [...(new Set(arr))];
Tests:
lodash/uniq
_.uniq(smalldata); _.uniq(bigdata);
filter+set
uniq(smalldata); uniq(bigdata);
array→set→array
uniqset(smalldata); uniqset(bigdata);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash/uniq
filter+set
array→set→array
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 break down the benchmark and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Overview** The benchmark compares three approaches to remove duplicate elements from an array: 1. Lodash's `uniq` function 2. A custom implementation using `filter`, `Set`, and `array→set→array` methods (let's call it "filter+set") 3. Another custom implementation using the spread operator (`uniqset`) followed by a set creation and then another spread operation (`array→set→array`) **Test Cases** The benchmark consists of three test cases: 1. **Lodash/uniq**: Tests Lodash's `uniq` function with two datasets: `smalldata` (a small array) and `bigdata` (a large array). 2. **Filter+set**: Tests the custom implementation using `filter`, `Set`, and `array→set→array` methods with both `smalldata` and `bigdata`. 3. **Array→set→array**: Tests the custom implementation using the spread operator, followed by a set creation and then another spread operation, with both `smalldata` and `bigdata`. **Options Compared** The three approaches have different characteristics: * Lodash's `uniq` function: + Pros: High-quality implementation, efficient, and well-tested. + Cons: External dependency on the Lodash library, which might be a concern for some users. * Filter+set: + Pros: Customizable, can be optimized for specific use cases. + Cons: More complex implementation, may have performance issues if not implemented correctly. * Array→set→array: + Pros: Simple and straightforward implementation, easy to understand. + Cons: May have performance issues due to the creation of a temporary set. **Library** In this benchmark, Lodash is used as a library. Lodash provides a high-quality implementation of various utility functions, including `uniq`. The use of Lodash allows for a fair comparison with custom implementations. **Special JS Features** There are no special JavaScript features or syntax used in this benchmark. All implementations rely on standard JavaScript methods and data structures. **Other Considerations** When choosing an implementation, consider the following factors: * Performance: If speed is critical, the `uniq` function from Lodash might be a better choice due to its optimized implementation. * Customizability: The filter+set approach provides more flexibility for users who want to optimize their code for specific use cases. * Readability and maintainability: The array→set→array implementation is relatively simple and easy to understand, making it a good choice for developers who prioritize code clarity. **Alternatives** If you're looking for alternative implementations or libraries, consider the following options: * Other set-based functions: Implementations like `Set.from()` from modern browsers or libraries like `fast-set` provide efficient set creation methods. * Array-based implementations: Alternative algorithms, such as the ones implemented in `filter+set`, can be optimized for specific use cases. Keep in mind that each implementation has its strengths and weaknesses. The choice ultimately depends on your project's requirements and your team's expertise.
Related benchmarks:
Lodash filter length vs sumby
uniqBy vs filter+set
Lodash difference vs filter and includes on large arrays
JS fastest unique array Set vs uniq vs filter
Comments
Confirm delete:
Do you really want to delete benchmark?