Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String.includes vs String.match vs String.indexOf
(version: 0)
Comparing performance of:
String.includes vs String.match vs String.indexOf
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
String.includes
const testStrFirst = 'foo'; "foo|bar".includes(testStrFirst); const testStrSecond = 'bar'; "foo|bar".includes(testStrSecond); const testStrNotMatch = 'baz'; "foo', 'bar'".includes(testStrNotMatch);
String.match
const testStrFirst = 'foo'; testStrFirst.match(/^(foo|bar)$/); const testStrSecond = 'bar'; testStrSecond.match(/^(foo|bar)$/); const testStrNotMatch = 'baz'; testStrNotMatch.match(/^(foo|bar)$/);
String.indexOf
const testStrFirst = 'foo'; "foo|bar".indexOf(testStrFirst); const testStrSecond = 'bar'; "foo|bar".indexOf(testStrSecond); const testStrNotMatch = 'baz'; "foo', 'bar'".indexOf(testStrNotMatch);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
String.includes
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):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **What is being tested?** The benchmark tests three different approaches for searching a substring within a string: `String.includes`, `String.match`, and `String.indexOf`. These methods are used to find the presence of a specific pattern or substring within a string. **Options compared:** 1. **`String.includes`**: This method uses the "in" operator to check if a specified value (the substring) is present in a string. It returns true if the value is found, and false otherwise. 2. **`String.match`**: This method uses a regular expression engine to search for a match within a string. The regular expression pattern is used to define what constitutes a match. 3. **`String.indexOf`**: This method finds the index of the first occurrence of a substring within a string. If the substring is not found, it returns -1. **Pros and Cons:** * `String.includes`: * Pros: * Simple to use * Fast * Does not require regular expressions * Cons: * Can be slow if used with large strings or complex patterns * May not work correctly for certain characters (e.g., Unicode) * `String.match`: * Pros: * More powerful and flexible than includes and indexOf, allowing for regular expression patterns * Can handle Unicode characters * Cons: * Requires knowledge of regular expressions, which can be complex to learn and use * May be slower than includes and indexOf due to the added complexity * `String.indexOf`: * Pros: * Fast and efficient for finding a substring within a string * Does not require regular expressions * Cons: * Returns an index, which may not be what the user expects if they're looking for a boolean result **Library and syntax considerations:** * The benchmark uses no external libraries other than JavaScript's built-in `String` methods. * No special JavaScript features or syntax are used beyond regular expressions (in the case of `String.match`). **Other alternatives:** * Regular expression engines like `RegExp` in JavaScript provide more advanced pattern matching capabilities but can be slower and more complex to use. * Other programming languages may have different string searching methods, such as `strpos` or `find`, which could potentially offer similar performance to the above options. In summary, this benchmark compares three built-in JavaScript methods for finding a substring within a string. While each method has its advantages and disadvantages, understanding their differences is essential for choosing the right approach for specific use cases.
Related benchmarks:
Regexp vs. Includes vs. IndexOf
IndexOf vs Includes on string
String.IndeOf vs. String.includes
String equals vs String.includes
.includes() vs indexOf() for single-character search in string
Comments
Confirm delete:
Do you really want to delete benchmark?