'foobar'.includes('foo');
'qwertyuiopfoobar'.includes('foo');
'bazbar'.includes('foo');
const regexp = /foo/;
'foobar'.match(regexp);
'qwertyuiopfoobar'.match(regexp);
'bazbar'.match(regexp);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.includes | |
String.match |
Test name | Executions per second |
---|---|
Array.includes | 11147573.0 Ops/sec |
String.match | 5294765.0 Ops/sec |
I'd be happy to help you understand the provided benchmark.
Benchmark Overview
The test compares two approaches for matching strings against multiple possibilities: using the includes()
method on arrays and strings, and using regular expressions with the match()
method.
Options Compared
There are two options being compared:
includes()
method on arrays to check if a string is present in the array.match()
method to search for a pattern (in this case, "foo") in a string.Pros and Cons of Each Approach
includes()
due to regular expression overhead.Library and Special JS Features
The test does not explicitly mention any libraries or special JavaScript features. However, it assumes a basic understanding of the includes()
method and regular expressions in JavaScript.
Other Considerations
When choosing between these approaches, consider the following:
Array.includes
might be a better choice.String.match
with regular expressions might be a better choice.Alternatives
Other alternatives for matching strings against multiple possibilities include:
Keep in mind that these alternatives may have their own trade-offs and performance characteristics.