Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn
(version: 0)
Comparing performance of:
Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs function unique(arr)
Created:
3 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>
Script Preparation code:
var itemsCount = 1e2; var items = Array.from({ length: itemsCount }, () => Math.floor(Math.random() * itemsCount));
Tests:
Javascript Array.reduce/Array.indexOf
items.reduce((list, item) => list.indexOf(item) > -1 ? list : [...list, item], []);
Lodash Uniq
_.uniq(items)
function unique(arr)
function unique(arr) { var hash = {}, result = []; for ( var i = 0, l = arr.length; i < l; ++i ) { if ( !hash.hasOwnProperty(arr[i]) ) { //it works with objects! in FF, at least hash[ arr[i] ] = true; result.push(arr[i]); } } return result; } unique(items)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Javascript Array.reduce/Array.indexOf
Lodash Uniq
function unique(arr)
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):
**Overview of the Benchmark** The provided JSON represents a JavaScript benchmark that compares three approaches for creating an array with unique values: `Array.reduce()` with `Array.indexOf`, Lodash's `uniq` function, and a custom implementation using an object hash. **Approaches Compared** 1. **JavaScript Array.reduce() with Array.indexOf**: This approach uses the `reduce()` method to iterate over the array and `indexOf()` to check for existing values. If a value is not found in the array, it's added to the result. 2. **Lodash Uniq**: Lodash provides a `uniq` function that removes duplicates from an array. This implementation simply calls this function on the input array. 3. **Custom Implementation using Object Hash**: This approach uses an object hash to keep track of unique values. It iterates over the array, checks if each value is already in the hash, and adds it to the result if not. **Pros and Cons** * **JavaScript Array.reduce() with Array.indexOf**: + Pros: Simple to implement, widely supported by browsers. + Cons: Has a time complexity of O(n^2) due to the use of `indexOf()` inside the loop, which can lead to poor performance for large arrays. * **Lodash Uniq**: + Pros: Fast and efficient, provides a reliable way to remove duplicates. + Cons: Adds an extra dependency on Lodash, may not be suitable for small projects or situations where dependencies are not allowed. * **Custom Implementation using Object Hash**: + Pros: Can be optimized for performance by using a hash table data structure, provides good performance for large arrays. + Cons: Requires more code and setup compared to the other approaches. **Library and Its Purpose** Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, string manipulation, and more. In this benchmark, Lodash's `uniq` function is used to remove duplicates from an array. **Special JS Feature or Syntax** This benchmark does not explicitly use any special JavaScript features or syntax beyond the standard ECMAScript features supported by most modern browsers. **Other Alternatives** * **Using a Set**: Instead of using an object hash, you could also use a `Set` data structure to keep track of unique values. This approach is more concise and efficient than the custom implementation. * **Using the `Promise.all()` Method**: If you need to process an array in parallel, you could use the `Promise.all()` method to create an array of promises that resolve to the unique values. Overall, this benchmark provides a good comparison of three approaches for creating an array with unique values, highlighting the trade-offs between simplicity, performance, and dependency management.
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 vs custom2
Uniq by sorting test
Uniq by sorting test 2
Comments
Confirm delete:
Do you really want to delete benchmark?