Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes 3
(version: 0)
Comparing performance of:
RegEx.test vs String.includes
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Hello world!"; var regex = /hello/gi;
Tests:
RegEx.test
regex.test(string);
String.includes
string.toLowerCase().includes(("Hello").toLowerCase());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
5170867.5 Ops/sec
String.includes
14014126.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. The benchmark is comparing two approaches to test if the string "Hello" is present anywhere in another given string: 1. **RegExp.test() method**: This method uses regular expressions (regex) to search for a pattern in a string. The regex `/hello/gi` is used, which: * `h` and `e` and `l` are literal characters. * `g` flag makes the search global, meaning it will find all occurrences of the pattern, not just the first one. * `i` flag makes the search case-insensitive. 2. **String.includes() method**: This method checks if a specified value (in this case, the lowercased string "Hello") is present anywhere in the original string. **Comparison Options** The benchmark compares these two approaches: 1. **RegExp.test() method**: This approach uses a regex pattern to search for the target string. 2. **String.includes() method**: This approach converts the target string to lowercase and then checks if it's present in the original string. **Pros and Cons of Each Approach:** 1. **RegExp.test() method**: * Pros: + More flexible, as regex patterns can be complex and powerful. + Can handle multiple matches, not just the first one. * Cons: + Can be slower for simple string searches due to the overhead of creating a regex pattern. 2. **String.includes() method**: * Pros: + Faster for simple string searches since it uses a built-in implementation. + More intuitive, as it's a standard JavaScript method. * Cons: + Less flexible, as it only works with exact matches (case-sensitive). + Requires converting the target string to lowercase, which can be unnecessary if case-insensitivity is not required. **Library and Purpose:** There are no libraries used in this benchmark. Both methods rely on built-in JavaScript functionality. **Special JS Feature or Syntax:** None mentioned. **Other Alternatives:** For simple string searches, other approaches could include: 1. **String.indexOf() method**: Similar to String.includes(), but returns the index of the first match instead of a boolean value. 2. **String.match() method**: Returns an array of matches if the target pattern is found anywhere in the string. These alternatives may offer different trade-offs between performance, flexibility, and readability, depending on the specific use case. The benchmark can be useful for developers to compare the performance of these approaches and choose the best fit for their project's requirements.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test vs String.includes
Long regex test vs string includes
Longer regex test vs string includes
Comments
Confirm delete:
Do you really want to delete benchmark?