Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes in array
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = ['image/heic', 'image/heif']
Tests:
IndexOf
array.indexOf('image/heic') !== -1
Includes
array.includes('image/heic')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
IndexOf
Includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0
Browser/OS:
Firefox 150 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
42239012.0 Ops/sec
Includes
42675316.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and analyze what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of two methods in JavaScript: `indexOf()` and `includes()`. Both methods are used to search for a specific value within an array. The goal is to determine which method is faster, more efficient, or less prone to errors. **Script Preparation Code** The script preparation code defines an array with two elements: ```javascript var array = ['image/heic', 'image/heif']; ``` This array will be used as the search space for both `indexOf()` and `includes()` methods. **Options Compared** Two options are being compared: 1. **`array.indexOf('image/heic') !== -1`**: This uses the `indexOf()` method to find the index of a specific value within the array, and then checks if it's not equal to `-1`, indicating that the value was found. 2. **`array.includes('image/heic')`**: This uses the `includes()` method directly, without searching for an index. **Pros and Cons** * `indexOf()`: + Pros: Can be more efficient when you need to find the exact index of a value in the array. + Cons: May return `-1` if the value is not found, which can lead to errors if you're expecting a specific result. * `includes()`: + Pros: More concise and expressive than using `indexOf()` with a check for `-1`. + Cons: Can be slower for very large arrays, since it needs to search the entire array. **Library** There is no explicit library being used in this benchmark. However, both methods are built-in JavaScript functions. **Special JS Feature/Syntax** None are explicitly mentioned in this benchmark. **Other Considerations** * Both methods have different performance characteristics: `indexOf()` tends to be faster for small arrays, while `includes()` can be slower for very large arrays. * If you're searching for a specific value that's likely to exist within the array (e.g., a common file extension), using `indexOf()` with a check for `-1` might be a better choice. **Alternatives** Other alternatives for string matching in JavaScript include: * Using a library like Lodash or Ramda, which provide more powerful and flexible string manipulation functions. * Using regular expressions to search for patterns within strings. * Using other data structures, such as hash tables or trie data structures, to store and query arrays. Overall, this benchmark is designed to highlight the differences between two simple yet common JavaScript methods, allowing users to compare performance and choose the best approach for their specific use case.
Related benchmarks:
IndexOf vs Includes
IndexOf vs Includes str
IndexOf vs Includes vs find
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?