Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs String comparison
(version: 0)
Comparing performance of:
Using string comparison vs Using regex vs Using array includes
Created:
2 years ago
by:
Registered User
Jump to the latest result
Tests:
Using string comparison
const contentType = 'application/json'; const match = contentType === 'application/json' || contentType === 'application/json; charset=utf-8'; console.log(match);
Using regex
const RE_CONTENT_TYPE_JSON = new RegExp('^application/json(; charset=utf-8)?$', 'i'); const contentType = 'application/json'; const match = RE_CONTENT_TYPE_JSON.test(contentType); console.log(match);
Using array includes
const contentType = 'application/json'; const match = ['application/json', 'application/json; charset=utf-8'].includes(contentType); console.log(match);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Using string comparison
Using regex
Using array includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_1_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1.1 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.1.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using string comparison
1147695.1 Ops/sec
Using regex
1019981.1 Ops/sec
Using array includes
1115549.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explore what's being tested, compared, and considered. **Benchmark Overview** The test compares three approaches to check if a string matches a certain pattern: 1. **String comparison**: Using the `===` operator to compare the string with two possible values. 2. **Regular expression (regex)**: Using a regex pattern to match the string against a set of possible values. 3. **Array includes**: Using the `includes()` method to check if the string is present in an array of possible values. **Options Compared** The benchmark compares these three approaches: * **Pros and Cons:** + String comparison: - Pros: Simple, easy to understand, and fast (no overhead from regex). - Cons: Limited flexibility, may not work with all edge cases. + Regex: - Pros: Highly flexible, can match complex patterns, and works well with edge cases. - Cons: Slower due to the regex engine's overhead, more verbose configuration. + Array includes: - Pros: Fast, easy to use, and reliable for simple use cases. - Cons: Limited flexibility, may not work with all edge cases (e.g., duplicate values). * **Considerations:** + Performance: Regex can be slower due to the engine's overhead, while array includes is generally fast. String comparison is likely the fastest but may require more manual handling for edge cases. + Readability and Maintainability: Array includes might be easier to read and understand for simple use cases, while regex requires a deeper understanding of pattern syntax. **Library Used** None explicitly mentioned in the provided code. However, it's worth noting that the `RegExp` constructor is used in the regex approach. **Special JS Features or Syntax** There are no special features or syntax used in this benchmark. **Benchmark Preparation Code and Definition** The benchmark preparation code is empty (`"Script Preparation Code": null`), suggesting that the script is meant to be run directly without any setup. The `Html Preparation Code` is also empty, indicating that the benchmark doesn't require a specific HTML environment. **Other Alternatives** Other alternatives for string matching could include: * Using a dedicated library like `fast-regex` or `regex-js`, which aim to improve performance and accuracy over the built-in `RegExp`. * Implementing custom string matching algorithms, such as using a trie data structure. * Utilizing other programming languages or frameworks that offer better performance or flexibility for string matching. Keep in mind that these alternatives might not be directly comparable to the regex approach used here.
Related benchmarks:
RegExp.test() vs String.match()
RegEx.test vs. String.includes vs. String.match vs String.search
Comparing performance of: String.search vs String.match
Reuse Global Regex? RegEx.test vs. String.match vs. String.search
Regex vs string compare
Comments
Confirm delete:
Do you really want to delete benchmark?