Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
includes vs regex
(version: 0)
Comparing performance of:
includes vs startsWith
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
includes
const str = "ta35"; str.includes("t5")
startsWith
const str = "ta35"; new RegExp("t5".split('').join('.*'), 'i').test(str)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
startsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
83383232.0 Ops/sec
startsWith
2560368.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in this benchmark. **Benchmark Definition**: The provided JSON defines a simple benchmark that compares the performance of two different approaches: `includes()` and regular expressions (`RegExp`) to check if a string contains a certain substring. **Options Compared**: 1. **`includes()` method**: This is a built-in JavaScript method that checks if a string contains a specified value. It's implemented in native code, which means it's optimized for performance. 2. **Regular Expression (`RegExp`) approach**: This involves creating a regular expression object and then using the `test()` method to check if the input string matches the pattern. **Pros and Cons of Each Approach:** 1. **`includes()` method**: * Pros: Fast, efficient, and widely supported. * Cons: Only works with strings; may not be suitable for more complex patterns or character classes. 2. **Regular Expression (`RegExp`) approach**: * Pros: More flexible and powerful than `includes()`, can handle regular expressions, but slower due to the overhead of creating a RegExp object. * Cons: Slower than `includes()` in most cases; may be overkill for simple string matching. **Library Used**: There is no specific library mentioned in this benchmark. The `includes()` method and `RegExp` approach are built-in JavaScript features. **Special JS Feature/Syntax**: The benchmark uses the `includes()` method, which is a relatively recent addition to the JavaScript language (introduced in ES6). It's also worth noting that this benchmark does not use any specialized syntax or features like async/await, Promises, or Web APIs. **Other Alternatives:** 1. **`indexOf()` method**: This is another built-in JavaScript method for finding a substring within a string. While it may be faster than `includes()` in some cases, it's generally slower and less flexible. 2. **Third-party libraries**: There are several third-party libraries available that provide optimized regular expression matching or more advanced text manipulation features, such as UglifyJS, String.prototype.trim() or String.prototype.replace() Keep in mind that the best approach for a specific use case may depend on factors like performance requirements, code readability, and the desired level of flexibility.
Related benchmarks:
Regex vs string.includes 2 terms
Case insensitive RegEx.test vs. String.includes when string doesn’t match
RegEx.test (with inline regex) vs. String.includes vs. String.match
Longer regex test vs string includes
regex vs includes - case insensitive
Comments
Confirm delete:
Do you really want to delete benchmark?