<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script>
var arrayToDedup = []
// 15 character long "random" string
for (var i = 0; i <= 1000; i++) {
arrayToDedup.push(Math.random().toString(36).substring(2, 7) + Math.random().toString(36).substring(2, 7) + Math.random().toString(36).substring(2, 7));
}
_.uniq(arrayToDedup);
[new Set(arrayToDedup)]
R.uniq(arrayToDedup);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash Uniq | |
Javascript Set | |
Ramda Uniq |
Test name | Executions per second |
---|---|
Lodash Uniq | 16105.0 Ops/sec |
Javascript Set | 15527.5 Ops/sec |
Ramda Uniq | 5876.7 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Definition
The benchmark is designed to compare three approaches for removing duplicates from an array:
Options Compared
The benchmark is comparing the performance of these three approaches:
uniq
function from the Lodash library to remove duplicates.Set
constructor and its methods to convert an array to a set and then back to an array.uniq
function from the Ramda library to remove duplicates.Pros and Cons
Here's a brief summary of each approach:
Library and Syntax
The benchmark uses the following libraries:
There are no special JavaScript features or syntax being tested in this benchmark. The test cases use standard JavaScript array methods (e.g., push
, set
) and built-in data structures (arrays, sets).
Alternatives
If you're looking for alternative approaches to remove duplicates from an array, here are a few options:
filter
method with a callback function to remove duplicates: [...array].filter((value, index, self) => self.indexOf(value) === index)
removeDuplicates
functionKeep in mind that each approach has its own trade-offs and limitations, so it's essential to consider your specific use case and requirements when choosing an approach.