var url = 'https://www.google.com/matchthis/other/args'
url.includes('matchthis')
/matchthis/.test(url)
url.match(/matchthis/).length >= 0
url.indexOf('matchthis') >= 0
url.search('matchthis') >= 0
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
text.includes() | |
text.test() | |
text.match() | |
text.indexOf() | |
text.search() |
Test name | Executions per second |
---|---|
text.includes() | 1568332800.0 Ops/sec |
text.test() | 28498808.0 Ops/sec |
text.match() | 12492502.0 Ops/sec |
text.indexOf() | 1625002752.0 Ops/sec |
text.search() | 19803168.0 Ops/sec |
What is being tested?
MeasureThat.net is testing the performance of different string methods in JavaScript:
includes()
test()
match()
indexOf()
search()
These methods are compared on the same input string "url" which contains a substring "matchthis".
Options being compared:
The options being compared are the performance of each method on the same input string. This allows users to compare and see which method is faster in different scenarios.
Here's a brief overview of each option:
includes()
: Returns true
if the specified value is present in the string, otherwise returns false
.test()
: Returns true
if the regular expression passed as an argument matches the specified string, otherwise returns false
.match()
: Returns an array of strings if the regular expression passed as an argument matches the specified string, otherwise returns null
.indexOf()
: Returns the index of the first occurrence of the specified value in the string, or -1
if it is not found.search()
: Returns the index of the first occurrence of the specified value in the string, or -1
if it is not found.Pros and Cons:
Here's a brief overview of the pros and cons of each method:
includes()
: Pros - simple to use, efficient. Cons - may return false positives if the input string contains multiple occurrences of the substring.test()
: Pros - flexible, can be used for regular expression matching. Cons - may be slower than includes()
due to the overhead of regular expression compilation.match()
: Pros - can be used for complex pattern matching. Cons - returns an array, which can be slower than returning a simple boolean value.indexOf()
: Pros - efficient, can be used for exact string matching. Cons - may return -1
if the substring is not found, and may require additional processing to handle false negatives.search()
: Pros - similar to indexOf()
, but with more flexibility in terms of regular expression support. Cons - similar to indexOf()
.Library usage:
None of the provided benchmarks use any external libraries or frameworks.
Special JavaScript features or syntax:
There are no special JavaScript features or syntax used in this benchmark, other than the standard methods being compared.
Other alternatives:
If you're looking for alternative string methods in JavaScript, some options include:
startsWith()
: Returns true
if the string starts with the specified value.endsWith()
: Returns true
if the string ends with the specified value.split()
: Splits the string into an array of substrings using a specified separator.substr()
or substring()
: Returns a subset of the string using a specified start and end index.Keep in mind that the choice of string method depends on your specific use case and requirements.