Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String.match vs String.includes
(version: 1)
Comparing performance of:
String.match vs String.includes
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
globalThis.matches = 0;
Tests:
String.match
const testStrFirst = 'hafoo'; matches += testStrFirst.match(/foo/g) ? 1 : 0; const testStrNotMatch = 'fohao'; matches += testStrNotMatch.match(/foo/g) ? 1 : 0;
String.includes
const testStrFirst = 'hafoo'; matches += testStrFirst.includes("foo") ? 1 : 0; const testStrNotMatch = 'fohao'; matches += testStrNotMatch.includes("foo") ? 1 : 0;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String.match
String.includes
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):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is defined by two test cases: 1. `String.match` 2. `String.includes` These two methods are used to search for a substring within a string in JavaScript. **Options Compared** In this benchmark, we have two options being compared: * `String.match()` * `String.includes()` Both methods are used to find the first occurrence of a substring within a string. However, they differ in their implementation and behavior: * `String.match()` returns an array containing the matched result(s), or null if no match is found. * `String.includes()` returns a boolean value indicating whether the specified value is present in the string. **Pros and Cons** Here's a brief summary of the pros and cons of each option: * `String.match()` + Pros: - More flexible, as it can be used to search for multiple substrings. - Can return an array of matches if multiple occurrences are found. + Cons: - Returns null if no match is found, which may require additional checks. - May have performance implications due to the creation of an array. * `String.includes()` + Pros: - Simple and intuitive API. - No need to worry about handling multiple matches or returns. + Cons: - Less flexible than `match()`, as it only searches for a single substring. - Returns false if the value is not found, which may require additional checks. **Library and Special Features** Neither of these methods uses any libraries. However, it's worth noting that `String.match()` was introduced in ECMAScript 1999 (ES5), while `String.includes()` was introduced in ECMAScript 2015 (ES6). The latter method provides a more modern and expressive way to perform substring searches. **Other Considerations** In addition to the pros and cons of each method, it's also worth considering factors such as: * Performance: Both methods have their own performance characteristics. `match()` may have better performance for larger strings due to its ability to return an array of matches. * Readability: `includes()` is generally considered more readable and easier to understand than `match()`, especially for simple substring searches. **Alternatives** If you need a more advanced or specialized way to perform substring searches, you might consider using: * `RegExp`: A powerful regular expression engine that provides more features and flexibility than the built-in `match()` and `includes()` methods. * Third-party libraries: There are many third-party libraries available that provide additional functionality for string manipulation, such as search and replace operations. I hope this explanation helps!
Related benchmarks:
RegEx.test vs. String.includes vs. String.match insensitive
Comparing performance of: String.search vs String.match
Reuse Global Regex? RegEx.test vs. String.match vs. String.search
RegEx.matchAll vs includes with match
RegEx.test (with inline regex) vs. String.includes vs. String.match
Comments
Confirm delete:
Do you really want to delete benchmark?