Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash uniq vs Object unique keys vs Set
(version: 0)
Comparing performance of:
Lodash uniq vs Object unique key vs Set
Created:
2 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 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) }
Tests:
Lodash uniq
_.uniq(arr);
Object unique key
arr.uniqueString();
Set
new Set(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash uniq
Object unique key
Set
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash uniq
8817049.0 Ops/sec
Object unique key
3710283.0 Ops/sec
Set
6205658.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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**: 1. **Lodash uniq**: This method uses the popular Lodash library, which provides a versatile set of functions for functional programming tasks. In this case, it's used to remove duplicates from an array. 2. **Object unique key**: This approach creates a JavaScript object with keys equal to the values in the original array. It then returns an array containing only the unique keys (i.e., no duplicates). The idea is that by having a unique key for each value, we can easily filter out duplicates. 3. **Set**: A Set data structure in JavaScript is designed to store unique values without duplicates. We create a Set from the original array and then convert it back to an array. This approach takes advantage of the Set's built-in uniqueness guarantee. **Pros and Cons of Each Approach:** 1. **Lodash uniq**: * Pros: Easy to use, well-tested, and optimized for performance. * Cons: Requires external dependency (Lodash), may not be as lightweight as other options. 2. **Object unique key**: * Pros: Lightweight, easy to understand, and doesn't require external dependencies. * Cons: May have a higher overhead due to object creation and lookup, potentially slower than Set or Lodash uniq. 3. **Set**: * Pros: Fast, lightweight, and suitable for large datasets. * Cons: Requires conversion from array to Set and back, which can be slower than direct usage of Set. **Library Descriptions (if applicable):** 1. **Lodash**: A popular JavaScript library providing a comprehensive set of functional programming utilities, including the `uniq` function used in this benchmark. 2. **Set**: A built-in data structure in JavaScript designed to store unique values without duplicates. **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:** 1. Other libraries like `lodash-es` (a smaller, ES6-compatible version of Lodash) or `underscore` could be used as alternatives to Lodash. 2. Custom implementations using JavaScript's native data structures, such as `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.
Related benchmarks:
Lodash uniq vs Object keys
Lodash uniq vs Object keys unique
uniqBy vs stringify performance
lodash uniqBy vs custom uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?