Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String.includes vs String.match 2
(version: 0)
Matching a string against more than one possibility.
Comparing performance of:
String.includes vs String.match vs Multiple if Conditions
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
String.includes
const testStrFirst = 'foo'; 'foobar'.includes(testStrFirst); const testStrSecond = 'bar'; 'foobar'.includes(testStrSecond); const testStrNotMatch = 'baz'; 'foobar'.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
String.includes
String.match
Multiple if Conditions
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String.includes
234674720.0 Ops/sec
String.match
19227886.0 Ops/sec
Multiple if Conditions
214357904.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested. The benchmark is designed to measure the performance of three different approaches for matching a string against multiple possibilities: 1. **String.includes**: This method checks if a given string contains another string as a substring. It's a simple and efficient approach that works well for most use cases. 2. **String.match**: This method returns an array of matches, or null if no match is found. It can be used with regular expressions to match against multiple possibilities. 3. **Multiple if Conditions**: This approach uses nested if statements to check if the string contains one of several possible values. Now, let's discuss the pros and cons of each approach: **String.includes**: Pros: * Simple and efficient * Works well for most use cases * Fast execution Cons: * Can be slower than other approaches for large strings or many matches * May not be suitable for situations where exact matching is required **String.match**: Pros: * Can be used with regular expressions, which provide powerful pattern-matching capabilities * Suitable for situations where exact matching is required * Fast execution Cons: * Can be slower than String.includes for simple substring matching * Requires more code to implement, especially when using regular expressions * May not be suitable for very large strings or many matches **Multiple if Conditions**: Pros: * Simple and easy to understand * Suitable for situations where exact matching is required Cons: * Can be slower than other approaches due to the overhead of evaluating multiple conditional statements * Requires more code and can become unwieldy for complex matching scenarios * May not be suitable for large strings or many matches The provided benchmark results show that: * **String.includes** is the fastest approach, with an average of 1040880896.0 executions per second across all tests. * **String.match** is slower than String.includes but faster than Multiple if Conditions, with an average of 26094408.0 executions per second. * **Multiple if Conditions** is the slowest approach, with an average of 13737024.0 executions per second. Other alternatives to these approaches include: * Using a regular expression engine like RegExp, which provides more powerful pattern-matching capabilities but can also be slower than String.includes * Using a specialized string matching library or algorithm, such as the Knuth-Morris-Pratt (KMP) algorithm for substring matching * Using a JavaScript framework or library that provides optimized string matching capabilities, such as MongoDB's $regex operator As for special JS features or syntax used in this benchmark, there are none mentioned. However, it's worth noting that some modern JavaScript engines and browsers may provide additional features or optimizations for string matching, such as the use of SIMD instructions or optimized regular expression engines. Overall, the choice of approach depends on the specific requirements and constraints of the project. String.includes is often a good default choice due to its simplicity and efficiency, but more complex scenarios may require the use of String.match or other specialized libraries or algorithms.
Related benchmarks:
String equality vs includes
RegEx.test vs. String.includes vs. String.match insensitive
Case insensitive RegEx.test vs. String.includes when string doesn’t match
String equals vs String.includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?