Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String.match 4
(version: 0)
Matching a string against more than one possibility.
Comparing performance of:
Array.includes vs String.match vs Regexp.test
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.RE = /foo/;
Tests:
Array.includes
'foobar'.includes('foo'); 'qwertyuiopfoobar'.includes('foo'); 'bazbar'.includes('foo');
String.match
const regexp = /foo/; 'foobar'.match(regexp); 'qwertyuiopfoobar'.match(regexp); 'bazbar'.match(regexp);
Regexp.test
window.RE.test('foobar'); window.RE.test('qwertyuiopfoobar'); window.RE.test('bazbar');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.includes
String.match
Regexp.test
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):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark that tests three different approaches for matching strings against multiple possibilities: 1. `Array.includes()` 2. `String.match()` with a regular expression 3. `Regexp.test()` with a regular expression Each test case uses a specific string as input and compares the execution times of each approach. **Options Compared** The benchmark compares the following options: * `Array.includes()`: This method is used to check if an element exists in an array. * `String.match()`: This method is used to search for a pattern within a string. When used with a regular expression, it can match multiple possibilities. * `Regexp.test()`: This method is similar to `String.match()`, but it returns a boolean value instead of the matched substring. **Pros and Cons** Here's a brief overview of each approach: * **Array.includes()**: Pros: * Simple and efficient for arrays with a few elements. * Fast in most cases. * Cons: * Not designed for strings, may not work as expected. * May be slower than `String.match()` or `Regexp.test()` for large inputs. * **String.match()**: Pros: * Can match multiple possibilities with a single call. * Fast and efficient. * Cons: * May be slower than `Array.includes()` for small inputs. * Can return an array of matches, which may require additional processing. * **Regexp.test()**: Pros: * Similar to `String.match()`, but with a simpler interface. * Fast and efficient. * Cons: * Returns a boolean value instead of the matched substring. **Library: RegExp** The `RegExp` library is used in all three test cases. It provides support for regular expressions in JavaScript, allowing you to match patterns within strings. **Special JS Feature/Syntax** There are no special features or syntaxes used in this benchmark. The examples are simple and straightforward. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * **String.indexOf()**: Similar to `Array.includes()`, but designed for strings instead of arrays. * **String.prototype.replace()`: Can be used to search and replace patterns within strings. However, it may not provide the same level of performance as `String.match()` or `Regexp.test()`. * **Use a dedicated library**: Libraries like Lodash or Underscore.js provide functions for matching and searching strings, which may offer better performance and features than the built-in methods. Keep in mind that the choice of approach depends on your specific use case and requirements. The benchmark provided is meant to give you an idea of how different methods compare in terms of performance.
Related benchmarks:
RegEx.matchAll vs. String.indexOf vs. String.match
RegEx.test vs. String.includes vs. String.match vs String.match(regex)
startsWith or regex test
startsWith or regex test or indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?