var string = "Hello world!";
var regex = /Hello/;
regex.test(string);
string.toLowerCase().includes("Hello".toLowerCase());
string.match("Hello");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RegEx.test | |
String.includes | |
String.match |
Test name | Executions per second |
---|---|
RegEx.test | 10297054.0 Ops/sec |
String.includes | 25805750.0 Ops/sec |
String.match | 5402236.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Overview
The benchmark is comparing three different approaches to test whether a string contains a specific substring:
RegEx.test()
String.toLowerCase().includes()
String.match()
What's being tested?
Options compared:
RegEx.test()
: This method uses a regular expression to test whether the entire string matches a given pattern. In this case, it's testing for the presence of "Hello" at the beginning of the string.Pros and cons of each approach:
RegEx.test()
:String.includes()
if the regex is optimized.RegEx.test()
since it uses a built-in method. Also, it can handle case-insensitive matching more effectively.RegEx.test()
if the pattern is simple and can match whole words. It also allows for capturing groups and returning multiple matches.Library usage
None in this benchmark.
Special JS feature/syntax
There are no specific JavaScript features or syntax being tested in this benchmark. The focus is on comparing the performance of different string manipulation methods.
Alternatives
Other alternatives to measure string matching performance could include:
RegExp.test()
However, MeasureThat.net's approach provides a convenient and standardized way to compare the performance of different methods in a controlled environment.