Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.startsWith() vs .test() vs .match() vs .indexOf()
(version: 0)
Comparing performance of:
RegExp.test(String) vs String.match(RegExp) vs String.indexOf() vs String.startsWith()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var url = "https://www.google.com";
Tests:
RegExp.test(String)
/^https/.test(url)
String.match(RegExp)
url.match(/^https/) !== null
String.indexOf()
url.indexOf('https') === 0
String.startsWith()
url.startsWith('https')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
RegExp.test(String)
String.match(RegExp)
String.indexOf()
String.startsWith()
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):
**Benchmark Overview** The provided benchmark compares the performance of four string methods in JavaScript: 1. `startsWith()` 2. `.test()` 3. `.match()` 4. `.indexOf()` These methods are used to check if a string starts with a certain value or pattern. **Method Comparison** Here's a brief overview of each method and their pros and cons: ### 1. `startsWith()` `startsWith()` returns `true` if the string starts with the specified value, and `false` otherwise. Pros: * Simple and intuitive * Fast Cons: * Only checks for exact match at the beginning of the string Example usage: `url.startsWith('https')` ### 2. `.test()` `.test()` returns `true` if the string matches the given regular expression, and `false` otherwise. Pros: * Flexible (can use regular expressions) * Fast Cons: * Can be slower than other methods for simple checks * May have performance issues with complex regex patterns Example usage: `/^https/.test(url)` ### 3. `.match()` `.match()` returns an array of matches if the string matches the given pattern, or `null` otherwise. Pros: * Flexible (can use regular expressions) * Can return multiple matches Cons: * May be slower than other methods for simple checks * Returns an array of matches, which can be more memory-intensive Example usage: `url.match(/^https/)` ### 4. `.indexOf()` `.indexOf()` returns the index of the first occurrence of the specified value in the string, or `-1` if not found. Pros: * Fast (especially for simple checks) * Returns an integer index Cons: * Only checks for exact match * May be slower for longer strings Example usage: `url.indexOf('https')` **Library and Special Features** None of the test cases use a library, but they do rely on JavaScript's built-in string methods. There are no special features or syntax used in these benchmark tests. The focus is solely on comparing the performance of the four string methods. **Alternative Approaches** Other alternatives to compare string matching include: * Using `includes()` method (introduced in ECMAScript 2019) * Using `regex.test()` and `regex.exec()` methods (available in most modern browsers) These alternatives may offer different trade-offs in terms of performance, complexity, and readability. However, they are not included in this specific benchmark. **Benchmark Results Interpretation** The provided benchmark results show the execution rate of each test case per second for a given browser and device platform. The highest execution rate is indicative of the fastest method. To interpret these results, consider the following: * `String.indexOf()` performed best on this dataset, indicating its high performance for simple string matching. * `.test()` and `.match()` performed similarly, but with slightly lower execution rates than `indexOf()`. * `startsWith()` performed slower than the other methods, likely due to its simpler implementation. Keep in mind that these results may vary depending on the specific test data, browser version, and device platform used.
Related benchmarks:
.includes() vs .test() vs .match() vs .indexOf() vs .search() fix
.startsWith() vs .test() vs .includes() vs .indexOf()
.includes() vs .test() vs .match() vs .indexOf() (w/ -1 != and !==)
.includes() vs .test() vs .match() vs .indexOf() begin
Comments
Confirm delete:
Do you really want to delete benchmark?