Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regexp vs. Includes vs. IndexOf
(version: 0)
Comparing performance of:
Regexp vs IndexOf vs Includes
Created:
5 years ago
by:
Guest
Go to the latest result
Tests:
Regexp
console.log(/s/.test('test'))
IndexOf
console.log('test'.indexOf('s') !== -1)
Includes
console.log('test'.includes('s'))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Regexp
IndexOf
Includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0
Browser/OS:
Firefox 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
IndexOf
274K/s
Includes
273K/s
Regexp
268K/s
View exact numbers
Test name
Executions per second
✓
IndexOf
274,143 Ops/sec
Includes
272,971 Ops/sec
Regexp
268,318 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark in detail. **What is being tested?** The provided JSON represents a JavaScript microbenchmark that compares three different approaches to check if a substring exists within a string: regular expressions (`/s/.test('test')`), `indexOf()` method, and `includes()` method. These three methods are compared because they have different performance characteristics, and the benchmark aims to determine which one is faster in various scenarios. **Options being compared** 1. **Regular Expressions (Regexp)**: This approach uses a regular expression pattern (`/s/`) to match the substring `'s'` within the string `'test'`. The `/.test()` method returns a boolean value indicating whether the pattern matches. 2. **indexOf() Method**: This approach uses the `indexOf()` method of the string object, passing the substring `'s'` as an argument. If the substring is found, the method returns its index; otherwise, it returns -1. 3. **includes() Method**: This approach uses the `includes()` method of the string object, passing the substring `'s'` as an argument. If the substring exists within the string, the method returns a boolean value indicating this. **Pros and Cons** * **Regular Expressions (Regexp)**: * Pros: Can match complex patterns, is widely used in various libraries and frameworks. * Cons: Performance can be slower due to the overhead of compiling regular expressions. It also requires more memory for pattern compilation. * **indexOf() Method**: * Pros: Fastest approach among the three, as it's optimized for string comparisons. * Cons: May not be as flexible as Regexp or includes(), as it only searches for a specific substring at a specific index. * **includes() Method**: * Pros: More readable and concise than Regexp, efficient enough for simple use cases. It's also part of the modern JavaScript standard. * Cons: May be slower than indexOf() in performance-critical scenarios due to its overhead. **Library/ Framework consideration** In this benchmark, none of the approaches rely on a specific library or framework other than built-in JavaScript methods and regular expressions. However, the usage of `includes()` method is more modern and part of the ECMAScript standard (ES6+), which might make it more readable and efficient for newer browsers. **Special JS feature/Syntax** There are no special JS features or syntaxes being tested in this benchmark. The focus is solely on the performance comparison of three different string comparison approaches. **Alternatives** If you want to create similar benchmarks, consider testing other string comparison methods like: 1. **Substring replacement**: Using a function that replaces all occurrences of a substring with another value. 2. **String concatenation and loop iteration**: Measuring the time taken by concatenating strings in a loop or using a specific concatenation technique (e.g., `join()`). 3. **Regular expression with different flags**: Comparing performance with different regular expression flags (e.g., `g` for global matching, `m` for multiline mode). Creating benchmarks helps to identify performance hotspots and optimize code, making your applications faster and more efficient.
Related benchmarks
RegEx.test vs String.includes vs String.indexOf
Case insensitive RegEx.test vs. String.includes when string doesn’t match
RegEx.test (with inline regex) vs. String.includes vs. String.match
regex vs includes - case insensitive
Comments
Confirm delete:
Do you really want to delete benchmark?