Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf > -1 vs Includes in string
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var string = '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 consequat.'
Tests:
IndexOf
string.indexOf('tempor') > -1
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:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
124981216.0 Ops/sec
Includes
122232976.0 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 Definition** The benchmark is designed to compare two approaches for searching a string: `string.indexOf()` and `string.includes()`. The goal is to measure which approach is faster in this specific use case. **Script Preparation Code** The script preparation code defines a variable `string` containing a long text string. This string will be used as the input for both searches. **Html Preparation Code** There is no HTML preparation code provided, so it's likely that the benchmark is focused solely on JavaScript performance. **Individual Test Cases** There are two test cases: 1. **IndexOf**: The first test case measures the execution time of `string.indexOf('tempor') > -1`. This will return a boolean value indicating whether the substring 'tempor' is found in the original string. 2. **Includes**: The second test case measures the execution time of `string.includes('tempor')`. This method returns a boolean value indicating whether the substring 'tempor' is found anywhere in the original string. **Comparison** The two approaches being compared are: * `string.indexOf()`: This method finds the index of the first occurrence of the specified substring. If the substring is not found, it returns -1. * `string.includes()`: This method checks if a specified value is present within the string. It returns true if the value is found, and false otherwise. **Pros and Cons** Here are some pros and cons of each approach: * **`string.indexOf()`**: + Pros: Faster for most cases, as it only needs to find the first occurrence. + Cons: May return -1 even if the substring appears multiple times in the string, which can be misleading. It also doesn't provide any information about the position of the found substring. * **`string.includes()`**: + Pros: Returns a boolean value and provides information about the presence of the substring (true or false). + Cons: Slower for most cases, as it needs to search the entire string. In this specific benchmark, `string.indexOf()` is likely expected to be faster because it only needs to find the first occurrence of 'tempor', whereas `string.includes()` needs to check every character in the string. **Library/ Framework Considerations** There isn't a specific library or framework mentioned in the benchmark definition. However, if we were to extend this benchmark to include other libraries or frameworks, we might need to consider factors like: * Browser-specific APIs and implementation differences * Module systems (e.g., ES6 imports) vs. CommonJS exports * String normalization and encoding differences **Special JS Features/Syntax** There doesn't appear to be any special JavaScript features or syntax being tested in this benchmark. The code only uses standard JavaScript constructs like variables, strings, and conditional statements. **Alternatives** If you wanted to modify this benchmark to explore different approaches, here are some alternatives: * Compare `string.indexOf()` with a custom implementation using loops or regular expressions. * Measure the performance of `string.includes()` on smaller vs. larger input strings. * Test the performance of both methods on different types of data (e.g., numbers, dates). * Explore browser-specific optimizations like caching or memoization for these string search operations. By experimenting with these alternatives, you can gain a deeper understanding of JavaScript performance optimization techniques and how to apply them in your own code.
Related benchmarks:
IndexOf vs Includes in Larger string
IndexOf vs Includes in string with check
IndexOf vs Includes in string as boolean
IndexOf vs Includes in string 2
IndexOf vs Includes in string - larger string edition
Comments
Confirm delete:
Do you really want to delete benchmark?