Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniq indexOf vs lodash uniq
(version: 0)
Comparing performance of:
ld uniq vs index of vs set
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var unique = (value, index, self) => { return self.indexOf(value) === index; }; var items = []; for (i = 0; i < 1000000; i++) { items.push(Math.floor(Math.random() * 100)); }
Tests:
ld uniq
_.uniq(items)
index of
items.filter(unique);
set
[...new Set(items)];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
ld uniq
index of
set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
ld uniq
61.6 Ops/sec
index of
33.6 Ops/sec
set
58.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmark results. **Benchmark Definition** The `Script Preparation Code` section defines two small JavaScript functions: 1. `unique`: This is an arrow function that takes three arguments: `value`, `index`, and `self`. It returns `true` if the `value` is found at its current `index` in the array (`self.indexOf(value) === index`). This function is not directly used in the benchmark but serves as a starting point for creating a custom filter. 2. The variable `items` is an array of 1,000,000 random integers. The `Html Preparation Code` section is empty, which means there's no additional setup required for the benchmark. **Individual Test Cases** There are three test cases: 1. `_.uniq(items)`: This test case uses Lodash's `uniq` function to remove duplicates from the `items` array. 2. `items.filter(unique)` : This test case filters the `items` array using a custom filter function (`unique`) that we defined earlier. 3. `[...new Set(items)]`: This test case converts the `items` array to an array of unique values using the spread operator and a new `Set`. **Comparison of Approaches** 1. Lodash's `uniq`: * Pros: Efficient, reliable, and widely tested. * Cons: Adds external dependency (Lodash), might not be suitable for all use cases. 2. Custom filter function (`unique`): * Pros: Tailored to the specific problem, no external dependencies. * Cons: May not be as efficient or reliable as Lodash's `uniq`, requires more code and testing effort. 3. Using a `Set`: * Pros: Simple, efficient, and widely supported. * Cons: Can be slower than Lodash's `uniq` for very large datasets. **Special JS Features** None are explicitly mentioned in the benchmark definition or test cases. However, using Lodash requires understanding its API and how to import it (e.g., `import _ from 'lodash';`) since it's not a built-in JavaScript function. **Other Alternatives** If you don't want to use Lodash, you could: 1. Use the Array.prototype.filter() method with a custom callback function. 2. Implement a set data structure using a custom array or object. 3. Utilize other library functions for unique value extraction (e.g., `Set` or `Map`). Keep in mind that each approach has its trade-offs and might not be suitable for all scenarios. In this benchmark, Lodash's `uniq` appears to be the fastest method, but it's essential to understand the pros and cons of each approach before choosing one.
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
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn vs custom2
Uniq by sorting test
Uniq by sorting test 2
Comments
Confirm delete:
Do you really want to delete benchmark?