Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Create an array with unique values - Lodash Uniq vs Javascript Set vs filter
(version: 0)
Comparing performance of:
Lodash Uniq vs Javascript Set vs JavaScript .filter()
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
Tests:
Lodash Uniq
let counter = 1e5; const arr = []; while(counter--) { arr.push(i); arr.push(i); } _.uniq(arr);
Javascript Set
let counter = 1e5; const set = new Set(); while(counter--) { set.add(i); set.add(i); } [...set];
JavaScript .filter()
let counter = 1e5; const arr = []; while(counter--) { arr.push(i); arr.push(i); } function onlyUnique(value, index, self) { return self.indexOf(value) === index; } arr.filter(onlyUnique);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash Uniq
Javascript Set
JavaScript .filter()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash Uniq
393.0 Ops/sec
Javascript Set
700.8 Ops/sec
JavaScript .filter()
798.3 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 their pros and cons. **Benchmark Overview** The benchmark measures the performance of three approaches to remove duplicates from an array: 1. Lodash Uniq 2. JavaScript Set 3. `.filter()` with a custom callback function (`onlyUnique`) **Options Compared** * **Lodash Uniq**: Uses the `uniq` function from Lodash, which removes duplicate values from an array while preserving order. * **JavaScript Set**: Uses a `Set` object to store unique values and then converts it to an array using the spread operator (`[...set]`). * **`.filter()` with custom callback**: Uses a custom callback function (`onlyUnique`) to filter out duplicates from the array. **Pros and Cons** 1. **Lodash Uniq**: * Pros: Efficient and easy to use, especially for larger datasets. * Cons: Requires additional dependencies (Lodash), and its performance may be slower than native JavaScript approaches. 2. **JavaScript Set**: * Pros: Fast and efficient, as it uses a hash table data structure under the hood. * Cons: May not preserve order of elements if the array is large, and requires understanding how to use `Set` objects correctly. 3. **`.filter()` with custom callback**: * Pros: Lightweight and easy to understand, as it only relies on standard JavaScript functions. * Cons: May be slower than using a native data structure like a `Set`, especially for larger datasets. **Library and Its Purpose** The benchmark uses Lodash, a popular JavaScript library that provides utility functions for tasks like array manipulation, string formatting, and more. In this case, the `uniq` function is used to remove duplicates from an array. **Special JS Feature or Syntax** None of the approaches rely on any special JavaScript features or syntax beyond standard JavaScript functions and data structures. **Benchmark Preparation Code** The benchmark preparation code includes: * A script tag that loads Lodash (version 4.17.5) * The `HTML` code is empty, which suggests that this benchmark may be run in a headless browser environment **Alternatives** Other alternatives for removing duplicates from an array include: * Using the `Array.prototype.reduce()` method with a callback function * Using the `Array.prototype.map()` and `Array.prototype.filter()` methods with a callback function * Using a library like Underscore.js or Ramda, which provide similar functions to Lodash Keep in mind that the choice of approach depends on the specific requirements of the project, such as performance, readability, and maintainability.
Related benchmarks:
unique elements in array using filter
lodash uniqBy vs custom uniqBy
Lodash - uniq2
returns unique values in array
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?