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
(version: 0)
Comparing performance of:
Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Created:
4 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)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Javascript Array.reduce/Array.indexOf
Lodash Uniq
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 and its test cases. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches for creating an array with unique values: `Array.reduce` combined with `Array.indexOf` (using JavaScript) and Lodash's `uniq` function. **Script Preparation Code** The script preparation code generates a random array of 100 unique integers using the `Array.from` method. This array is used as input for both test cases. ```javascript var itemsCount = 1e2; // 100 var items = Array.from({ length: itemsCount }, () => Math.floor(Math.random() * itemsCount)); ``` **Html Preparation Code** The HTML preparation code includes the Lodash library, which is used in one of the test cases. ```html <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script> ``` **Test Cases** There are two test cases: ### Test Case 1: JavaScript Array.reduce/Array.indexOf This test case uses the `reduce` method in combination with `Array.indexOf`. The idea is to iterate over the array and add each item to the result array if it's not already present. ```javascript items.reduce((list, item) => list.indexOf(item) > -1 ? list : [...list, item], []); ``` **Pros:** * Native JavaScript implementation, which might be optimized for performance. * Easy to understand and implement. **Cons:** * The `indexOf` method can lead to a linear search in the worst case (when an item is not found), resulting in a time complexity of O(n). * This approach may be slower than using a more efficient algorithm like Lodash's `uniq`. ### Test Case 2: Lodash Uniq This test case uses Lodash's `uniq` function to create an array with unique values. ```javascript _.uniq(items); ``` **Pros:** * More efficient and optimized implementation compared to the JavaScript approach. * Lodash's implementation might be faster due to its use of specialized algorithms or data structures. **Cons:** * Requires including the Lodash library, which may add overhead. * Might not be as easy to understand for those without familiarity with Lodash. **Other Considerations** * The benchmark is executed on a Chrome 100 browser on a Linux desktop platform. The results might vary depending on the environment and browser version used. * The `ExecutionsPerSecond` value indicates the number of times each test case was executed per second, which can affect the overall performance measurement. **Alternative Approaches** Other approaches to create an array with unique values could include: * Using a Set data structure (native JavaScript): `new Set(items).values();` * Utilizing a library like Ramda or Underscore.js for more functional programming-inspired solutions. * Implementing a custom algorithm using bitwise operations or other optimization techniques. Keep in mind that the performance of these alternatives might vary, and it's essential to test and benchmark them to determine their suitability for your specific use case.
Related benchmarks:
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?