<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var arr = ['Jane', 'Bob', 'Kat', 'Kat', 'Bob', 'Jane', 'Ian', 'Jane', 'Bob', 'Kat', 'Kat', 'Bob', 'Jane', 'Ian'];
Array.prototype.uniqueString = function() {
let tmpObj = {}
for (var i = 0; i < this.length; i++) {
tmpObj[this[i]] = this[i]
}
return Object.values(tmpObj)
}
_.uniq(arr);
arr.uniqueString();
new Set(arr)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash uniq | |
Object unique key | |
Set |
Test name | Executions per second |
---|---|
Lodash uniq | 8817049.0 Ops/sec |
Object unique key | 3710283.0 Ops/sec |
Set | 6205658.0 Ops/sec |
Let's break down what is being tested in the provided benchmark.
Benchmark Definition: The benchmark compares three different approaches to remove duplicate values from an array: Lodash's uniq
function, an implementation of unique keys using JavaScript objects (Object unique key
), and a simple approach using a Set data structure (Set
).
Options Compared:
Pros and Cons of Each Approach:
Library Descriptions (if applicable):
uniq
function used in this benchmark.Special JS Features/Syntax:
This benchmark does not require any special JavaScript features or syntax. It's written in standard JavaScript and uses a common array manipulation approach.
Other Alternatives:
lodash-es
(a smaller, ES6-compatible version of Lodash) or underscore
could be used as alternatives to Lodash.Map
, could also be considered as alternatives to the Set
approach.In summary, this benchmark compares three approaches to remove duplicates from an array: a well-tested and optimized library function (Lodash uniq
), a lightweight but potentially slower object-based implementation (Object unique key
), and a fast and efficient Set-based solution.