Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
r includes vs regex
(version: 1)
Comparing performance of:
regex vs includes
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
let regex = /a/
Tests:
regex
regex.test('bbbbbbbaaaaa')
includes
'bbbbbbbaaaaa'.includes('a')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regex
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
regex
57488392.0 Ops/sec
includes
209983872.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark in question compares two different approaches for determining the presence of a character (in this case, the character 'a') within a string: using the `String.prototype.includes()` method and using a regular expression (`RegExp`). ### Option Comparison 1. **Includes Method:** - **Test Case:** `'bbbbbbbaaaaa'.includes('a')` - **Purpose:** This method checks if a specific substring exists within the string and returns a boolean value (`true` or `false`). - **Performance:** As seen in the benchmark results, this method achieved around 209,983,872 executions per second, indicating that it is quite efficient. - **Pros:** - Simpler syntax and more readable for those who are familiar with high-level string operations. - Returns a straightforward boolean that can be used directly in conditional checks. - **Cons:** - This approach is limited to checking for substrings rather than complex patterns. 2. **Regular Expression:** - **Test Case:** `regex.test('bbbbbbbaaaaa')` - **Purpose:** This method uses a regular expression to check for matches in a string. In this case, the regex `/a/` checks for the presence of the character 'a'. - **Performance:** The benchmark results indicate that this method reached approximately 57,488,392 executions per second. - **Pros:** - More powerful and flexible, allowing for pattern matching, character classes, and a wide variety of complex string searching tasks. - **Cons:** - More complex syntax that may be less readable for those unfamiliar with regular expressions. - Potential performance overhead compared to simple substring checks for basic tasks like character presence. ### Libraries and Special Syntax In this benchmark, no external libraries are directly utilized other than JavaScript's built-in capabilities. The test cases leverage the native `String.prototype.includes()` method and the RegExp test method, both of which are standard features of JavaScript. Neither test case uses special JavaScript features or syntax that deviates from standard practices. Both approaches are well-established and commonly used. ### Other Alternatives Apart from the `includes` method and regular expressions, there are other ways to check for the existence of a substring, including: - **Using `indexOf()`:** ```javascript 'bbbbbbbaaaaa'.indexOf('a') !== -1 ``` - **Pros:** Supported in older browsers and widely understood. - **Cons:** Slightly less readable than `includes()`. - **Using `Array.prototype.some()`:** ```javascript [...'bbbbbbbaaaaa'].some(char => char === 'a'); ``` - **Pros:** More functional programming style, suitable for more complex conditions. - **Cons:** Less efficient for simple substring checks compared to `includes()` or regex. ### Conclusion In summary, the benchmark effectively compares two popular string searching techniques in JavaScript. The `includes()` method is more straightforward and performs better for simple character checks, while regular expressions provide unmatched flexibility for more complex string pattern matching. Each option has its use case, and the choice between them should be guided by the specific requirements of the task at hand.
Related benchmarks:
Regexp vs. Includes vs. IndexOf
Array.includes vs Regex.test
booger
Regex vs split/includes
RegEx.test vs String.includes
Long regex test vs string includes
String.includes vs Regex.test
regex.test vs. array.includes
starts with vs regex
Comments
Confirm delete:
Do you really want to delete benchmark?