Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String in Array: Set vs IndexOf vs includes vs findIndex vs find
(version: 0)
Comparing performance of:
indexOf vs includes vs find vs findIndex vs set
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 10) arr[i] = ('' + i++); var testSet = new Set(...arr); var randomKeys = []; i = 0; while (i <= 50) { randomKeys.push(''+Math.round(Math.random(50000) + 5000)); i++; }
Tests:
indexOf
for (const key of randomKeys) {const index = arr.indexOf(key);}
includes
for (const key of randomKeys) {const index = arr.includes(key);}
find
for (const key of randomKeys) {const index = arr.find(item => item == key);}
findIndex
for (const key of randomKeys) {const index = arr.findIndex(item => item == key);}
set
for (const key of randomKeys) {const index = testSet.has(key);}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
indexOf
includes
find
findIndex
set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
1004689.8 Ops/sec
includes
864999.1 Ops/sec
find
789672.9 Ops/sec
findIndex
694449.8 Ops/sec
set
8104565.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of different methods for finding an element in an array: `indexOf`, `includes`, `findIndex`, `find`, and `set`. The test creates a large array with 51 elements (0-50) and a set containing all these elements. It then iterates over this set using various methods to find the presence of each element. **Tested Options** Here are the options being compared: 1. **indexOf**: Uses the `indexOf` method on the original array. 2. **includes**: Uses the `includes` method on the original array. 3. **findIndex**: Uses the `findIndex` method on the original array, which returns the index of the first occurrence of a specified value. 4. **find**: Uses the `find` method on the original array, which returns the first element in an array that satisfies a provided condition. 5. **set**: Uses the `has` method on the set to check if an element is present. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **indexOf**, **includes**, and **findIndex**: These methods are generally fast because they use an array index-based search algorithm. However, their performance can degrade if the array is very large or has duplicate elements. 2. **find**: This method uses a linear scan of the array to find the first occurrence of the specified value. It's slower than `indexOf` and `includes`, especially for larger arrays. 3. **set**: The `has` method on the set is particularly efficient because it only needs to check if the element is present in the set, without having to iterate over the entire array. **Library** In this benchmark, the `Set` class from JavaScript's built-in `Object` namespace is used. A set is a collection of unique values, which makes it ideal for fast membership testing (i.e., checking if an element is already present). **Special JS Features/Syntax** There are no specific JavaScript features or syntax used in this benchmark beyond what's required by the standard library (e.g., `Set`, `indexOf`, etc.). **Alternatives** If you wanted to write a similar benchmark, you could consider using other data structures, such as: * A custom implementation of a set or hash table * An array with duplicate elements * A more complex data structure, like a trie or a suffix tree However, keep in mind that these alternatives might not accurately represent the performance characteristics of your specific use case. For a similar benchmark, you could also explore using different programming languages or frameworks to compare their performance on this specific microbenchmark.
Related benchmarks:
findIndex vs indexOf - JavaScript performance
findIndex vs indexOf for simple array 2
findIndex vs indexOf vs includes - JavaScript performance
String in Array: Set vs IndexOf vs includes vs findIndex vs find v2
Comments
Confirm delete:
Do you really want to delete benchmark?