Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Perf test
(version: 4)
Comparing performance of:
indexOf vs _.uniq vs Set vs Custom
Created:
6 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js"></script>
Script Preparation code:
var max1 = 5000; var arr1 = []; for (var i = 0; i <= max1; i++) { arr1.push(faker.internet.domainName()); } console.log(arr1);
Tests:
indexOf
const unique = arr1.filter((i, pos) => arr1.indexOf(i) === pos);
_.uniq
const unique = _.uniq(arr1);
Set
const unique = [...new Set(arr1)];
Custom
function filterLists(arr) { const a = arr.concat(); for (let i = 0; i < a.length; i += 1) { for (let j = i + 1; j < a.length; j += 1) { if (a[i] === a[j]) { a.splice(j -= 1, 1); } } } return a; } const unique = filterLists(arr1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
indexOf
_.uniq
Set
Custom
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):
**Benchmark Explanation** The provided JSON represents a JavaScript benchmark test suite, which is used to measure the performance of different algorithms for removing duplicates from an array. **Script Preparation Code** The script preparation code defines two variables: `max1` and `arr1`. `max1` is set to 5000, which will be used as the upper limit for generating a random array. `arr1` is initialized as an empty array and populated with random domain names using the Faker library. **Html Preparation Code** The HTML preparation code includes two external script tags that load the Lodash and Faker libraries, respectively. **Test Cases** There are four test cases: ### 1. `indexOf` This test case uses the built-in `indexOf` method to filter out duplicate elements from the array. The performance of this approach is expected to be slow due to its linear search nature. Pros: Simple and straightforward implementation. Cons: Linear search makes it inefficient for large arrays. ### 2. `_uniq` (Lodash) This test case uses the Lodash library's `uniq` function to remove duplicates from the array. The performance of this approach is expected to be faster than the built-in `indexOf` method due to its optimized implementation. Pros: Optimized implementation, likely to be faster than `indexOf`. Cons: Requires an external library (Lodash). ### 3. `Set` This test case uses a JavaScript `Set` object to remove duplicates from the array. The performance of this approach is expected to be efficient because sets use a hash table data structure under the hood. Pros: Efficient data structure, likely to be fast. Cons: May require additional memory allocation for the set. ### 4. `Custom` This test case uses a custom implementation that iterates through the array and removes duplicates by using `splice` to shift elements back into place. The performance of this approach is expected to be slower than the optimized implementations of `_uniq` and `Set`. Pros: Educational value, can help illustrate the concept of removing duplicates. Cons: Slow implementation due to unnecessary iterations. **Library Descriptions** * Lodash: A popular utility library for JavaScript that provides a wide range of functions for working with arrays, objects, and more. The `uniq` function is used in this benchmark to remove duplicates from an array. * Faker: A library for generating fake data, including random domain names, which are used in the script preparation code. **Special JS Features** None mentioned explicitly in the provided information. However, it's worth noting that JavaScript supports various features such as arrow functions, `let` and `const` declarations, and template literals, but none of these are explicitly used in this benchmark. **Alternatives** Other alternatives for removing duplicates from an array include: * Using a library like `uniqsort` or ` duplicate-check` * Implementing a custom algorithm using a data structure like a trie or a suffix tree * Using a streaming approach with a pipeline of functions to process the array Keep in mind that each alternative has its own trade-offs and may not offer better performance or simplicity than the existing implementations.
Related benchmarks:
Test Array Speed
Test native unique
Lodash.ceil vs Math.ceil
Sherlock Benchmarks v6
Comments
Confirm delete:
Do you really want to delete benchmark?