Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique array items comparison
(version: 0)
Comparing performance of:
Set() vs lodash uniq vs native filter
Created:
5 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()
var l = new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]); return [...l];
lodash uniq
var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return _.uniq(l);
native filter
var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return l.filter((v, i, self) =>{ return i == self.indexOf(v) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set()
lodash uniq
native filter
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/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set()
7812397.0 Ops/sec
lodash uniq
23622034.0 Ops/sec
native filter
9908993.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and the pros/cons of different approaches. **Benchmark Overview** The benchmark tests three ways to compare an array for duplicate elements: 1. Using a native `Set` data structure 2. Using the popular JavaScript library Lodash's `uniq` function 3. Using a native `filter` method with a custom condition **Tested Approaches** Each test case represents one of these approaches. ### 1. Native Set Data Structure (`var l = new Set([...])`) This approach uses a native `Set` data structure to store unique elements from the input array. The `return [...l];` statement converts the `Set` back into an array, which is then returned by the benchmark. Pros: * Lightweight and efficient * Built-in functionality in modern JavaScript engines Cons: * May not be as fast as other approaches for very large arrays due to the overhead of creating a `Set` * Not all browsers support `Set` natively (e.g., older versions of Internet Explorer) ### 2. Lodash `uniq` Function (`var l = [...]; return _.uniq(l);`) This approach uses Lodash's `uniq` function, which removes duplicate elements from an array. Pros: * Fast and efficient, especially for large arrays * Portable across different browsers and environments, as long as Lodash is included Cons: * Requires including Lodash in the project, which can add extra dependencies * May not be suitable for projects with strict size or weight constraints ### 3. Native `filter` Method (`var l = [...]; return l.filter((v, i, self) =>{ ... });`) This approach uses a native `filter` method to remove duplicate elements from the array. Pros: * Fast and efficient, especially for large arrays * Built-in functionality in modern JavaScript engines Cons: * May not be as readable or maintainable as other approaches due to the complexity of the filtering condition * Can be slower than Lodash's `uniq` function for very large arrays due to the overhead of creating a filter callback **Other Considerations** * The benchmark measures the number of executions per second, which can affect the results. A higher number of executions may indicate better performance. * The test cases are run in Chrome 118 on Windows Desktop, which may not be representative of other environments or browsers. **Alternative Approaches** Some alternative approaches to removing duplicate elements from an array include: * Using `Array.prototype.reduce()` and an accumulator object to remove duplicates * Using a library like Ramda's `unique` function (similar to Lodash's `uniq`) * Implementing a custom algorithm using bitwise operations or other optimization techniques However, these alternatives may not be as widely supported or efficient as the approaches tested in this benchmark.
Related benchmarks:
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn
Lodash - uniq
Lodash - uniq2
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?