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 v2
(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:
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The provided benchmark compares the performance of four different approaches for searching an array: `set`, `indexOf`, `includes`, and `findIndex`. The test case creates an array with random keys, and then uses each approach to find a specific key in the array. The results are compared across multiple browsers and devices. **Approach 1: Using a Set (`set`)** * **Purpose**: A `Set` is a data structure that stores unique values. In this test, it's used to store the array keys. * **Pros**: + Fast lookup times, as sets use hash tables for storage and retrieval. + Efficient use of memory, as only unique values are stored. * **Cons**: + Requires additional memory allocation, which might impact performance in certain scenarios. + May have slower initialization times compared to other approaches. **Approach 2: `indexOf`** * **Purpose**: The `indexOf` method searches for a specific value in an array and returns the index of its first occurrence. If the value is not found, it returns -1. * **Pros**: + Well-established, widely supported method with predictable performance. + Simple implementation, making it easy to compare with other approaches. * **Cons**: + May have slower lookup times compared to `includes` and `findIndex`, especially for large arrays. **Approach 3: `includes`** * **Purpose**: The `includes` method checks if a value is present in an array. It returns `true` if the value is found, and `false` otherwise. * **Pros**: + Fast lookup times, similar to `indexOf`. + Simplified implementation compared to `findIndex`, making it easier to compare with other approaches. * **Cons**: + May have slightly slower performance than `indexOf` due to the additional overhead of checking if the value is included. **Approach 4: `findIndex`** * **Purpose**: The `findIndex` method searches for a specific value in an array and returns the index of its first occurrence. If no value is found, it returns -1. * **Pros**: + Fast lookup times, similar to `indexOf`. + More flexible than `indexOf`, as it can be used with more complex conditions (e.g., callback functions). * **Cons**: + May have slightly slower performance compared to `includes` due to the additional overhead of using a function. **Library Used** None of the approaches rely on external libraries. However, some modern browsers might use internal implementation details or caching mechanisms that could impact the results. **Special JavaScript Feature/Syntax** The benchmark uses ES6 features (e.g., arrow functions, template literals) and recent syntax (e.g., `const` instead of `var`). The test is written in a way that it should be compatible with most modern browsers. **Alternatives** Other approaches to searching arrays could include: 1. Using the `Array.prototype.filter()` method to find values. 2. Implementing a custom binary search algorithm. 3. Utilizing data structures like heaps or balanced trees for efficient searching. 4. Leveraging WebAssembly or other low-level technologies to optimize performance. Keep in mind that these alternatives might have different trade-offs, such as increased complexity, memory usage, or compatibility issues. When choosing an approach, consider the specific requirements of your use case, including factors like performance, simplicity, and maintainability.
Related benchmarks:
indexOf vs findIndex with a simple case
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
Comments
Confirm delete:
Do you really want to delete benchmark?