Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf
(version: 0)
Comparing performance of:
index vs set
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
input = [2, 5, 3, 6, 1, 2, 3, 9, 4, 3, 2, 10, 20, 1, 2, 8, 8, 1, 9, 2, 2, 3, 4, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]
Tests:
index
unique = input.filter((v,i) => input.indexOf(v) === i)
set
unique = [...new Set(input)];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
index
set
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):
I'll break down the provided benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Overview** The benchmark is testing two approaches to find unique elements in an array: using the `indexOf()` method with an index-based comparison or creating a new Set from the input array. The test cases aim to determine which approach is faster on average. **Script Preparation Code** The script preparation code defines the input array: ```javascript input = [2, 5, 3, 6, 1, 2, 3, 9, 4, 3, 2, 10, 20, 1, 2, 8, 8, 1, 9, 2, 2, 3, 4, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; ``` This is a large array with repeated elements to ensure that the benchmarks are representative of real-world scenarios. **Html Preparation Code** The HTML preparation code is empty (`null`), indicating that this benchmark focuses solely on JavaScript execution speed and does not require any additional HTML setup or testing. **Individual Test Cases** There are two test cases: 1. **"index"`**: ```javascript unique = input.filter((v, i) => input.indexOf(v) === i); ``` This test case uses the `filter()` method with a callback function that checks if the index of each element (`i`) is equal to its index in the original array using the `indexOf()` method. 2. **"set"`**: ```javascript unique = [...new Set(input)]; ``` This test case creates a new Set from the input array and then converts it back to an array (using the spread operator) to get the unique elements. **Pros and Cons of Each Approach** 1. **"index"`**: * Pros: This approach is more efficient because it uses the `indexOf()` method, which is optimized for fast lookup times. * Cons: It requires a separate iteration over the input array to find the index of each element, which can be slower than creating a Set. 2. **"set"`**: * Pros: Creating a Set is generally faster because it uses a data structure optimized for quick membership testing (i.e., `add()` and `has()` operations). * Cons: Converting the Set back to an array requires additional overhead, which can negate some of the performance benefits. **Library** None of these benchmarks rely on any external libraries. The `Set` object is a built-in JavaScript data structure that's available in most modern browsers. **Special JS Features/Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's required for standard array operations (`filter()`, `indexOf()`). **Other Alternatives** In the absence of this specific benchmark, other approaches to find unique elements might include: * Using a data structure like a Trie (prefix tree) to store and query elements. * Utilizing parallel processing techniques, such as MapReduce or workers, to speed up computations on large datasets. Keep in mind that these alternatives might not be equally well-suited for small arrays like the one used in this benchmark.
Related benchmarks:
unique elements in array using filter - large array
Uint8Array4685231156412
filtering with Array.prototype.flatMap vs for of loop
filtering with Array.prototype.flatMap vs for of loop vs Array.prototype.map + Array.prototype.filter
Javascript native forEach vs _.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?