Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.includes() vs .test() vs .match() vs .indexOf() begin
(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='https://www.google.com/matchthis/other/args'
Tests:
text.includes()
url.includes('https://www.google.com')
regex.test()
/matchthis/.test(url)
string.match()
url.match(/matchthis/)
string.indexOf()
url.indexOf('https://www.google.com') >= 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain what's being tested in the provided JSON, compare different approaches, and discuss their pros and cons. **What is being tested?** The benchmark compares four different methods for finding a substring within a string: 1. `includes()`: A method introduced in JavaScript 13 (ECMAScript 2022) that allows you to check if a string includes another string. 2. `test()`: Not a standard JavaScript method, but rather a part of the `RegExp` object, which is used for regular expressions. 3. `match()`: Another method from the `RegExp` object, specifically designed for matching patterns in strings. 4. `indexOf()`: A built-in method that returns the index of the first occurrence of a substring within another string. **Comparison of approaches** Here's a brief overview of each approach: * `includes()`: + Pros: Fast and efficient, especially for large strings. It uses a binary search algorithm to find the substring. + Cons: Not supported in older JavaScript versions (before ECMAScript 2022). * `test()`: A method from the `RegExp` object that tests if a string matches a regular expression pattern. While it can be used for substring matching, its primary purpose is for more complex pattern matching. + Pros: Can be used with regular expressions to match patterns, not just substrings. + Cons: Can be slower than the other methods due to the complexity of the regular expression engine. * `match()`: Another method from the `RegExp` object that returns an array of matches if there are any. While it can be used for substring matching, its primary purpose is for finding multiple matches in a string. + Pros: Can be used with regular expressions to find multiple matches, not just substrings. + Cons: Can be slower than the `includes()` method due to the complexity of the regular expression engine and the need to create an array of matches. * `indexOf()`: A built-in method that returns the index of the first occurrence of a substring within another string. It's a simple, iterative approach that can be slow for large strings. + Pros: Built-in method, so it's available in all JavaScript environments. + Cons: Can be slower than the other methods due to its linear search algorithm. **Library and purpose** None of the methods use any external libraries or frameworks specifically designed for string matching. However, `RegExp` is a built-in object that provides regular expression functionality, including `test()` and `match()`. These methods can be used with additional libraries or frameworks if needed. **Special JS feature or syntax** The benchmark uses ECMAScript 2022 features (introduced in JavaScript 13), specifically the `includes()` method. This is a new feature introduced in recent versions of JavaScript, so it may not work in older environments. **Other alternatives** If none of these methods are suitable for your use case, you can consider using: * `String.prototype.startsWith()` and `String.prototype.endsWith()`: These methods can be used to check if a string starts or ends with another substring. * `Array.prototype.indexOf()`: This method can be used to find the index of a substring within an array of strings. In summary, while there are different approaches to finding substrings in JavaScript, `includes()` is generally the fastest and most efficient method for simple substring matching.
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 .toLowerCase().indexOf() with non-alpha 1
Comments
Confirm delete:
Do you really want to delete benchmark?