Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs String.match
(version: 0)
Matching a string against more than one possibility.
Comparing performance of:
Array.includes vs String.match vs Multiple if Conditions
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
Array.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)$/);
Multiple if Conditions
const testStrFirst = 'foo'; if (testStrFirst == 'foo' || testStrFirst == 'bar') {} const testStrSecond = 'bar'; if (testStrFirst == 'foo' || testStrFirst == 'bar') {} const testStrNotMatch = 'baz'; if (testStrFirst == 'foo' || testStrFirst == 'bar') {}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.includes
String.match
Multiple if Conditions
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
76279400.0 Ops/sec
String.match
15121363.0 Ops/sec
Multiple if Conditions
187776576.0 Ops/sec
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 Overview** The benchmark compares three approaches to check if a string matches multiple possibilities: 1. `Array.includes` 2. `String.match` with regular expressions 3. Multiple `if` conditions using equality checks (`==`) **Options Compared** * **Array.includes**: This method uses the `includes()` function to search for an element in an array. It returns `true` if the element is found and `false` otherwise. * **String.match** with regular expressions: This method uses a regular expression pattern to match the input string against multiple possibilities. The `/^(foo|bar)/` pattern matches any string that starts with either "foo" or "bar". * **Multiple if Conditions**: This approach uses equality checks (`==`) to compare the input string with multiple possibilities. **Pros and Cons** * **Array.includes**: + Pros: Simple, efficient, and widely supported. + Cons: May not be as flexible as regular expressions or may use more memory for large arrays. * **String.match** with regular expressions: + Pros: Flexible, powerful, and can handle complex matching patterns. However, it can be slower than `Array.includes` due to the overhead of compiling the regex pattern. + Cons: May be less intuitive for simple string matching tasks, requires knowledge of regex syntax. * **Multiple if Conditions**: + Pros: Easy to understand and implement, can be faster since it avoids regex compilation. + Cons: Inefficient, may lead to performance issues with large numbers of conditions. **Library and Special JS Features** * No external libraries are used in this benchmark. * The `includes()` method is a built-in JavaScript function. * Regular expressions (regex) are not explicitly mentioned as a special feature, but they are used in the `String.match` approach. If you're not familiar with regex, it's worth learning about them for more complex matching tasks. **Other Alternatives** If you need to perform string matching with regular expressions, consider using other libraries like `regex-escape` or `regex-test`. For simple string matching, you could also use a dedicated library like `string-match`. **Benchmark Considerations** When choosing an approach, consider the following factors: * Performance: If speed is critical, `Array.includes` might be the best choice. However, if flexibility and accuracy are more important, `String.match` with regular expressions could be the better option. * Readability and maintainability: For simple string matching tasks, multiple `if` conditions might be easier to understand than regular expressions or built-in functions like `includes()`. * Complexity: If you need to match complex patterns or edge cases, `String.match` with regular expressions is a good choice.
Related benchmarks:
equality vs includes
=== vs includes
String equals vs String.includes
equals vs includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?