var strArr = `airbnbairbnbjlhljhjh
airbnb
haha air bnb haha
airbnb
airbnbkjlkjlj`.split("\n");
var re1 = new RegExp('airbnb', 'i');
var re2 = new RegExp('.*airbnb.*', 'i');
for (var i = 0; i < strArr.length; i++) {
var res = re1.test(strArr[i]);
}
for (var i = 0; i < strArr.length; i++) {
var res = re2.test(strArr[i]);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 | |
2 |
Test name | Executions per second |
---|---|
1 | 1242119.1 Ops/sec |
2 | 631473.2 Ops/sec |
Let's break down the benchmark and explain what's being tested.
What is being tested?
The provided JSON represents two test cases that measure the performance of regular expression (regex) matching in JavaScript. The tests compare the execution speed of two different regex patterns: re1
and re2
.
Options being compared
There are two options being compared:
re1
is a simple regex pattern that matches the string "airbnb" in a case-insensitive manner ('airbnb', 'i'
). re2
is a more complex regex pattern that matches any string containing "airbnb" in a case-insensitive manner ('.*airbnb.*', 'i'
).RegExp
constructor.Pros and cons of each approach
re1
):re2
):Library usage
There is no explicit mention of a library being used in this benchmark. However, it's likely that the RegExp
constructor is used internally by the JavaScript engine to execute the regex patterns.
Special JS feature or syntax
The benchmark uses the .split()
method to split the input string into an array, which is then used in the test cases. This is a standard JavaScript method for splitting strings.
Other alternatives
If you wanted to write this benchmark using a different approach, some alternatives could be:
regex-test
or regex- performance
that provide more advanced features and tools for testing regex patterns.re2
with the u
flag or testing precompilation of regex patterns.I hope this explanation helps!