Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Case Insensitive RegEx.test vs. String.includes vs String.startsWith
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.startsWith
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Content-Security-Policy-Report-Only"; var regex = /^content-security-policy/i;
Tests:
RegEx.test
regex.test(string);
String.includes
string.toLowerCase().includes("content-security-policy");
String.startsWith
string.toLowerCase().startsWith("content-security-policy");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
String.startsWith
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):
Let's break down the provided JSON and explain what's being tested. **Benchmark Overview** The benchmark compares three different approaches for case-insensitive string matching in JavaScript: 1. `RegEx.test()` 2. `String.includes()` with `.toLowerCase()` 3. `String.startsWith()` with no explicit case conversion These three methods are compared on the same input string: `"Content-Security-Policy-Report-Only"`. **Options Compared** The benchmark is testing the performance of each method under different conditions: * `RegEx.test()`: a traditional regular expression approach * `String.includes()`: a method that uses the `includes()` function to check if a substring exists in a string, with `.toLowerCase()` applied to both strings for case-insensitive comparison * `String.startsWith()`: a method that checks if a string starts with another string, with no explicit case conversion **Pros and Cons of Each Approach** 1. **RegEx.test()**: * Pros: highly flexible, can match complex patterns, and is widely supported. * Cons: slower than native methods like `String.includes()` or `String.startsWith()`, requires more CPU resources. 2. **String.includes() with `.toLowerCase()`**: * Pros: fast, easy to use, and efficient for simple cases. * Cons: applies `.toLowerCase()` to both strings, which can be unnecessary and introduce additional overhead if the input string is already in a specific case. 3. **String.startsWith() with no explicit case conversion**: * Pros: optimized for start-of-string checks and avoids unnecessary case conversions like `.toLowerCase()`. * Cons: may not be as flexible as `RegEx.test()` or as efficient as `String.includes()` without `.toLowerCase()`. **Library and Special JS Features** None of the methods used in this benchmark rely on a specific library. However, `String.includes()` is a built-in JavaScript method that has been optimized for performance. There are no special JS features being tested in this benchmark, such as async/await or modern language features like `async/parallel` execution. **Other Alternatives** Some alternative approaches to testing string matching performance could include: * Using a different input string or dataset * Comparing the performance of other methods like `String.indexOf()`, `String.match()`, or `RegExp.test()` * Incorporating additional factors, such as cache or indexing overhead, into the benchmark Overall, this benchmark provides a simple and effective way to compare the performance of three common string matching approaches in JavaScript.
Related benchmarks:
RegEx.test vs. String.includes case insensitive
Case insensitive RegEx.test vs. String.includes when string doesn’t match
Case Insensitive RegEx.test vs. String.includes
regex vs includes - case insensitive
Comments
Confirm delete:
Do you really want to delete benchmark?