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 vs custom2
(version: 0)
Comparing performance of:
Javascript Array.reduce/Array.indexOf vs object literal/res.push(arr[i]); vs object literal/res.length
Created:
3 years ago
by:
Guest
Jump to the latest result
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], []);
object literal/res.push(arr[i]);
function unique(arr) { var hash = {}, result = []; for ( var i = 0, l = arr.length; i < l; ++i ) { if ( !hash.hasOwnProperty(arr[i]) ) { hash[ arr[i] ] = true; result.push(arr[i]); } } return result; } unique(items)
object literal/res.length
function unique(arr) { var hash = {}, result = []; for ( var i = 0, l = arr.length; i < l; ++i ) { if ( !hash.hasOwnProperty(arr[i]) ) { hash[ arr[i] ] = true; result[result.length] = 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
object literal/res.push(arr[i]);
object literal/res.length
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 the provided benchmark definition and test cases. **Benchmark Definition** The benchmark is designed to compare three approaches for creating an array with unique values: 1. `Array.prototype.reduce()`: This method reduces an array by applying a function to each element, returning the first value that satisfies the condition. 2. `Lodash Uniq` (a library): Lodash Uniq is a utility function from the Lodash library that removes duplicate elements from an array. 3. A custom implementation using an object literal and the `res.push()` method. 4. Another custom implementation using an object literal, but with a different approach (`result[result.length] = arr[i];`). **Options Compared** The benchmark compares these four approaches: * **Pros and Cons** + `Array.prototype.reduce()`: This approach is concise and efficient for creating an array of unique values. However, it may not be suitable for very large arrays due to performance overhead. + Lodash Uniq: This library provides a simple and reliable way to remove duplicates from an array. However, it may have a slight performance impact due to the additional function call. + Custom implementation with `res.push()`: This approach is straightforward but may lead to slower performance compared to other methods due to repeated `push()` calls. + Custom implementation with `result.length`: This approach is similar to the previous one but uses indexing instead of `push()`. It might have a slight performance advantage, but it's still not as efficient as `reduce()`. * **Other Considerations** + The benchmark measures the execution time per second (`ExecutionsPerSecond`), which indicates how many times each approach can be executed in 1 second. + The results may vary depending on the size of the input array and the specific JavaScript engine used by the browser. **Libraries and Special JS Features** * Lodash Uniq: This is a utility function from the Lodash library, which provides various utility functions for working with arrays, objects, and other data structures. * No special JS features are mentioned in the benchmark definition. **Alternatives** Other approaches to creating an array of unique values might include: * Using `Set` or `Map` data structures, which provide built-in functionality for removing duplicates. * Utilizing a sorting approach followed by an array filter method. * Implementing a custom solution using a hash table or other data structure. However, these alternatives are not explicitly tested in this benchmark definition.
Related benchmarks:
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Methods to remove duplicates from array (fork)
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn
Uniq by sorting test 2
Comments
Confirm delete:
Do you really want to delete benchmark?