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 indexofmap
(version: 2)
Comparing performance of:
Lodash Uniq vs Javascript Set vs Javascript indexOf
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
Script Preparation code:
let counter = 1e5; arr = []; while (counter--) { arr.push('test'); arr.push('test'); arr.push('test'); arr.push('test'); arr.push('test'); }
Tests:
Lodash Uniq
_.uniq(arr);
Javascript Set
[...new Set(arr)];
Javascript indexOf
arr.filter((v, i, s)=>s.indexOf(v)===i)
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 indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided benchmark. **Benchmark Overview** The benchmark is designed to measure the performance of three different approaches for creating an array with unique values: 1. Using Lodash's `uniq` function 2. Using JavaScript's built-in `Set` data structure 3. Using a custom implementation with `indexOf` and `filter` **Lodash Uniq** * Description: The benchmark script prepares an array of 100,000 elements, where each element is repeated four times. * Lodash Uniq is used to remove duplicates from the array. * Pros: + Easy to use and maintain + Provides a robust implementation with error handling * Cons: + Adds overhead due to the creation and management of an external library + May not be optimized for performance **JavaScript Set** * Description: The benchmark script prepares an array of 100,000 elements, where each element is repeated four times. * A `Set` data structure is used to remove duplicates from the array. * Pros: + Lightweight and fast, as it only stores unique values + Optimized for performance, with a low overhead * Cons: + Requires understanding of JavaScript's built-in data structures + May not provide the same level of error handling as Lodash Uniq **Javascript indexOf** * Description: The benchmark script prepares an array of 100,000 elements, where each element is repeated four times. * A custom implementation uses `indexOf` and `filter` to remove duplicates from the array. * Pros: + Can be optimized for performance using clever techniques + Provides a low overhead, as it only relies on built-in functions * Cons: + Requires manual effort to implement correctly + May not be as efficient or robust as Lodash Uniq or JavaScript Set **Library Used: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array manipulation, and more. In this benchmark, the `uniq` function is used to remove duplicates from an array. **Special JS Feature/Syntax** * **Rest Parameters**: The custom implementation uses rest parameters (`(...v)`) to define a callback function that takes multiple arguments. * **Arrow Functions**: The custom implementation uses arrow functions (`(i, s)=>s.indexOf(v)===i`) for simplicity and readability. **Other Alternatives** If you're looking for alternative approaches to creating an array with unique values, here are a few options: 1. Using `new Map()`: A map can be used to store unique values, where each value is associated with a key. 2. Using `Array.from()`**: The `from` function can be used to create an array from an iterable, such as a set or an object. 3. Manual implementation using `for...of` loops: You can use manual loops to iterate over the array and add unique values. Keep in mind that each approach has its pros and cons, and the best choice depends on your specific use case and performance requirements.
Related benchmarks:
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Create an array with unique values - Lodash Uniq vs Javascript Set vs filter
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn
lodash uniq vs set - 3
Comments
Confirm delete:
Do you really want to delete benchmark?