Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter a list to remove all duplicate values
(version: 0)
Comparing performance of:
Using indexOf vs Using lastIndexOf
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (let i = 0; i < 100000; i++) { array.push(Math.floor((Math.random() * 10) + 1)); }
Tests:
Using indexOf
array.filter((item, index) => array.indexOf(item) != index);
Using lastIndexOf
array.filter((item, index) => array.lastIndexOf(item) != index);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using indexOf
Using lastIndexOf
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 explain what's being tested in the provided JSON benchmark. **Benchmark Overview** The benchmark tests two different approaches to filter an array and remove all duplicate values: using `indexOf` and using `lastIndexOf`. The goal is to determine which approach is faster. **Script Preparation Code** The script preparation code creates an array of 100,000 random integers between 1 and 10. This array will be used as the input for the filter function. **Html Preparation Code** There is no HTML preparation code provided, so this step is skipped. **Individual Test Cases** There are two individual test cases: 1. **Using indexOf**: The benchmark definition uses the `indexOf` method to check if an item exists in the array. If it does not exist at the current index, the item is included in the filtered array. 2. **Using lastIndexOf**: The benchmark definition uses the `lastIndexOf` method to check if an item exists in the array. If it does not exist at the current index, the item is included in the filtered array. **Pros and Cons of Each Approach** * **Using indexOf**: + Pros: Generally faster than using `lastIndexOf`, since `indexOf` can return -1 immediately if the item is not found. + Cons: May be slower for large arrays with many duplicate values, as it has to search the entire array from the start. * **Using lastIndexOf**: + Pros: Can be faster when searching for a specific item in an array that contains many duplicates, since `lastIndexOf` can stop searching once it finds the first occurrence of the item. + Cons: May be slower than using `indexOf` overall, as it has to search the entire array from the start. **Other Considerations** * The benchmark does not account for the case where an item is not found in the array. In this case, both approaches will throw an error (although `indexOf` may throw a TypeError, while `lastIndexOf` may throw an RangeError). * The benchmark only tests these two approaches and does not consider other methods for removing duplicates from an array, such as using Set or using a custom implementation. **Library Used** None. This benchmark uses only built-in JavaScript functions: `array.filter()`, `indexOf()`, and `lastIndexOf()`. **Special JS Features or Syntax** This benchmark does not use any special JavaScript features or syntax that would be unfamiliar to most software engineers. **Alternatives** For removing duplicates from an array, other approaches include: * Using a Set: Create a Set from the array, which automatically removes duplicates. Then, convert the Set back to an array. * Using a custom implementation: Write a custom function to iterate through the array and remove any duplicate values. * Using the spread operator: Convert the array to an object using `Object.assign()`, then use the spread operator (`...`) to create a new array with unique elements. These alternatives may have different performance characteristics than the benchmark's two approaches, but can also be useful in certain situations.
Related benchmarks:
Methods to remove duplicates from array
Methods to remove duplicates from array (fork)
Methods to remove duplicates from array (test)
Methods to remove duplicates from array x2
Comments
Confirm delete:
Do you really want to delete benchmark?