Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes in Larger string
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = 'Lorem ipsum Lorem Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequatcommodo consequatea commodo consequatex ea commodo consequatipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequatdolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequatcommodo consequat.'
Tests:
IndexOf
string.indexOf('tempor')
Includes
string.includes('tempor')
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:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0
Browser/OS:
Firefox 115 on Windows 7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
505385216.0 Ops/sec
Includes
9178012.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is tested, compared options, pros and cons, and other considerations. **Benchmark Definition** The benchmark definition represents a microbenchmark that tests the performance of two different JavaScript functions: `string.indexOf()` and `string.includes()`. The test string is quite long, containing multiple occurrences of the word "tempor". **Script Preparation Code** The script preparation code creates a large string variable `string` with the provided text. This string will be used for both tests. **Html Preparation Code** There is no HTML preparation code provided, which means that only JavaScript performance is being tested and compared. **Individual Test Cases** There are two individual test cases: 1. **IndexOf**: Tests the performance of `string.indexOf('tempor')`. The purpose of this function is to find the index of the first occurrence of the specified value ("tempor") in the string. 2. **Includes**: Tests the performance of `string.includes('tempor')`. This function returns a boolean indicating whether the string contains the specified value. **Options Compared** The two options being compared are: * `indexOf`: Returns the index of the first occurrence of the value, or -1 if not found. * `includes`: Returns a boolean indicating whether the string contains the value. If the value is found, returns true; otherwise, returns false. **Pros and Cons** Here's a brief analysis of each option: * **IndexOf**: + Pros: Generally faster than `includes` since it only needs to find the index of the first occurrence. + Cons: May return -1 if the value is not found, which can be useful for some use cases but can also lead to false positives if not handled correctly. * **Includes**: + Pros: Returns a clear boolean result (true or false), making it easier to handle edge cases. + Cons: Generally slower than `indexOf` since it needs to search the entire string. **Other Considerations** * Both tests are comparing performance, so the test results will show which function is faster and more efficient for this specific use case. * The test string size is quite large, which may impact performance. In a real-world scenario, smaller strings would likely be used for such tests. * The `includes` method has become a more modern and convenient alternative to `indexOf` in many cases. However, its performance might not be optimal for every use case. **Library Usage** Neither of the functions uses any external libraries in this test case. **Special JS Features or Syntax** None are explicitly mentioned, but it's worth noting that both `indexOf` and `includes` utilize JavaScript's built-in string methods.
Related benchmarks:
slice vs substr vs substring (with end index & large string)
Javascript index vs substring
Javascript: Case insensitive string comparison performance 3
IndexOf vs Includes in string - larger string edition
Comments
Confirm delete:
Do you really want to delete benchmark?