Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
url.includes() vs .test() vs .match() vs .indexOf()
(version: 0)
Compare different intra-string matching styles
Comparing performance of:
text.includes() vs regex.test() vs string.match() vs string.indexOf()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var url='/sports/nba'
Tests:
text.includes()
url.includes('/sports/nba')
regex.test()
/\/sports\/(nba)/.test(url)
string.match()
url.match(/\/sports\/(nba)/)
string.indexOf()
url.indexOf('/sports/nba') >= 0
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
text.includes()
regex.test()
string.match()
string.indexOf()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
text.includes()
13770596.0 Ops/sec
regex.test()
8742252.0 Ops/sec
string.match()
7179322.0 Ops/sec
string.indexOf()
13833514.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is tested, compared, pros, cons, and other considerations. **Benchmark Definition** The benchmark compares different intra-string matching styles: `url.includes()`, `.test()`, `.match()`, and `.indexOf()`. These methods are used to search for a specific pattern within a string. The difference lies in how they approach the search: * `includes()`: Returns `true` if the string includes the specified value, without asserting equality. * `.test()`: Similar to `includes()`, but can be more efficient and flexible when using regular expressions (regex). * `.match()`: Returns an array of matches if the string matches a regex pattern. Can be useful for more complex patterns. * `.indexOf()`: Returns the index of the first occurrence of the specified value, or -1 if not found. **Pros and Cons** Here's a brief summary of each method: * `includes()`: + Pros: Simple, straightforward, and fast. + Cons: May return false positives (e.g., `"hello".includes("world")"` returns `true`). * `.test()`: + Pros: Efficient, flexible with regex, and accurate. + Cons: May be slower than `includes()` for simple cases. * `.match()`: + Pros: Powerful for complex patterns, can return multiple matches. + Cons: Can be slower and more memory-intensive than other methods. * `.indexOf()`: + Pros: Fast and efficient, accurate. + Cons: May return -1 if the value is not found, which can lead to errors. **Library and Special JS Features** None of the benchmarked methods rely on specific JavaScript libraries or features beyond standard JavaScript syntax. However, the use of regular expressions (.test() and .match()) may be unfamiliar to some developers, especially those without experience with regex. **Other Considerations** When choosing an intra-string matching method, consider: * Performance: If speed is crucial, `.indexOf()` might be a good choice. For more complex patterns, `.test()` or `.match()` might be better. * Accuracy: If exact matches are required, `.includes()` and `.indexOf()` can help avoid false positives. * Flexibility: If you need to search for multiple patterns or handle edge cases, `.test()` and `.match()` provide more flexibility. **Alternatives** If you don't have access to the same JavaScript engine as the benchmark (e.g., browser vs. Node.js), consider alternative methods: * For simple string matching: Use `string.prototype.includes()` in browsers or a custom implementation. * For regex patterns: Use a dedicated library like `regex` in Node.js or a browser's built-in regex API. Keep in mind that the JavaScript engine and available features may impact performance differences between these alternatives.
Related benchmarks:
.includes() vs .test() vs .match() vs .indexOf()
.includes() vs .test() vs .match() vs .indexOf() (w/ -1 != and !==)
.includes() vs .test() vs .match() vs .indexOf()asdasd
.includes() vs .test() vs .match() vs .indexOf() begin
Comments
Confirm delete:
Do you really want to delete benchmark?