Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
include vs indexof
(version: 0)
Comparing performance of:
indexOf vs includes
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = ['daggers', 'firehoses', 'beast', 'sakaar', 'datapier']; if(arr.indexOf('da') >-1){console.log('Done')}
Tests:
indexOf
var arr = ['daggers', 'firehoses', 'beast', 'sakaar', 'datapier']; if(arr.indexOf('da') >-1){console.log('Done')}
includes
var arr = ['daggers', 'firehoses', 'beast', 'sakaar', 'datapier']; if(arr.includes('da')){console.log('Done')}
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:
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, the options compared, their pros and cons, and other considerations. **Benchmark Overview** The benchmark is designed to compare two approaches: using `indexOf` vs `includes` methods for searching an array in JavaScript. The test creates a sample array with five elements and checks if a specific string ('da') exists within it using both methods. **Script Preparation Code** ```javascript var arr = ['daggers', 'firehoses', 'beast', 'sakaar', 'datapier']; ``` This code defines an array `arr` containing five strings. The intention is to use this array in the subsequent test cases. **Html Preparation Code (none)** There is no HTML preparation code provided, which means that the benchmark only focuses on JavaScript execution and does not involve any web page rendering or DOM manipulation. **Individual Test Cases** The benchmark has two individual test cases: 1. `indexOf` 2. `includes` Both tests use the same array definition as in the Script Preparation Code. **Test Case 1: indexOf** ```javascript if(arr.indexOf('da') >-1){console.log('Done')} ``` This code uses the `indexOf` method to search for 'da' within the `arr` array. The `> `-1` check is a common pattern to ensure that the string exists in the array, returning -1 if it's not found. **Test Case 2: includes** ```javascript if(arr.includes('da')){console.log('Done')} ``` This code uses the `includes` method to search for 'da' within the `arr` array. The `includes` method returns a boolean value indicating whether the string exists in the array. **Benchmark Results** The benchmark results are: 1. `includes`: 90156456.0 executions per second 2. `indexOf`: 88431832.0 executions per second These results indicate that, on this specific hardware configuration (Chrome 83 on Mac OS X 10.14.3), the `includes` method is slightly faster than the `indexOf` method. **Pros and Cons of Each Approach** 1. **indexOf** * Pros: Generally considered more efficient for searching arrays, as it only searches forward in the array. * Cons: May be slower if the search string is not a prefix of another element's value (e.g., searching for 'da' in an array with non-ASCII values). 2. **includes** * Pros: More concise and readable than `indexOf`, as it eliminates the need to check the return value. * Cons: May be slower due to the additional overhead of parsing the string and checking for equality. **Library/Features** There is no specific library or feature being tested in this benchmark. However, both methods are part of the built-in JavaScript API. **Special JS Features/Syntax** None mentioned in this explanation. **Alternatives** Some alternative approaches to comparing array search methods include: 1. Using `findIndex` or `find` instead of `indexOf`/`includes`. 2. Implementing a custom binary search algorithm for arrays. 3. Using a different data structure, such as a set or map, that supports faster lookup and insertion operations. These alternatives would require significant changes to the benchmark code and may not provide comparable results due to their differences in behavior and performance characteristics.
Related benchmarks:
elielieli
elielielieli
IndexOf vs Includes str
Array find with indexOf vs includes
JS indexOf vs some
Comments
Confirm delete:
Do you really want to delete benchmark?