Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes in string 333
(version: 0)
Banana 333
Comparing performance of:
IndexOf vs Includes
Created:
5 years ago
by:
Guest
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('tempor123') > -1
Includes
string.includes('tempor123')
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:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
1562948352.0 Ops/sec
Includes
1561895552.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain what's being tested in the provided JSON benchmark. **Benchmark Purpose** The test aims to measure and compare the performance of two string comparison methods: `indexOf` and `includes`. The focus is on determining which method is faster when searching for a specific substring within a large string. **Options Compared** Two options are compared: 1. **`string.indexOf('tempor123') > -1`**: This uses the `indexOf` method, which returns the index of the first occurrence of the specified value (in this case, 'tempor123'), or -1 if it's not found. 2. **`string.includes('tempor123')`**: This uses the `includes` method, which returns a boolean value indicating whether the string includes the specified value ('tempor123'). **Pros and Cons** * **`indexOf` Method:** * Pros: * Can be more efficient when searching for exact matches. * Returns an index value immediately if found. * Cons: * May perform poorly if not optimized (e.g., if the string is large or has many non-matching characters). * **`includes` Method:** * Pros: * More readable and expressive code. * Handles edge cases like empty strings more elegantly. * Cons: * May be slower than `indexOf` due to the overhead of searching for substring presence. **Library** The test uses the built-in JavaScript `String.prototype.indexOf()` and `String.prototype.includes()` methods, which are part of the ECMAScript standard. These methods are implemented by most modern browsers and Node.js environments. **Special JS Feature/Syntax (None in this case)** There's no use of special JavaScript features or syntax like async/await, promises, or ES6+ features that require specific browser support. **Other Alternatives** Some alternative approaches to benchmarking string comparison performance include: * Using a different substring search algorithm (e.g., Boyer-Moore or Knuth-Morris-Pratt). * Testing with different types of strings (e.g., numbers, dates, regex patterns). * Evaluating the impact of specific optimization techniques on string comparison performance (e.g., caching results, using memoization). Keep in mind that these alternative approaches might require additional setup and configuration to ensure accurate results.
Related benchmarks:
IndexOf vs Includes in Larger string
IndexOf vs Includes in string with check
Js Search - String StartsWith vs IndexOf vs Includes
IndexOf vs Includes in string 2
Comments
Confirm delete:
Do you really want to delete benchmark?