var string = "HelaasdfgafsdfsfdsadfsadfsadsadgfdgaesssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssHello world!";
var regex = /Hello/;
regex.test(string);
string.includes("Hello");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
regex | |
includes |
Test name | Executions per second |
---|---|
regex | 5820136.0 Ops/sec |
includes | 14563225.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition
The benchmark definition is represented by two JSON objects: Script Preparation Code
and Html Preparation Code
. These codes are executed before running the actual benchmark test.
In this case, we have:
Script Preparation Code
: This code defines a string variable string
containing the text "Hello world!" and a regular expression variable regex
with the pattern "/Hello/".Html Preparation Code
: This field is empty, indicating that no HTML preparation is needed for this benchmark.Options being compared
The two test cases being compared are:
regex.test(string);
string.includes("Hello");
These two tests are designed to measure the performance of JavaScript's built-in regular expression engine versus a simple string inclusion method using the includes()
method.
Pros and Cons
Here's a brief analysis of each approach:
regex.test(string);
):string.includes("Hello");
):includes()
method may not work as expected if the search string is not found at the beginning of the string.Library and syntax
There are no notable libraries being used in this benchmark, but we can observe that the regular expression engine is using a specific syntax (/Hello/
).
As for special JavaScript features or syntax, there's no explicit mention of any advanced features like async/await, Promises, or ES6+ syntax.
Other alternatives
If you're looking to optimize string inclusion operations, some alternative approaches could include:
indexOf()
method instead of includes()
, as it may be faster in some cases.includes
function, which is optimized for performance.Keep in mind that these alternatives would require additional code and testing to confirm their performance benefits.
I hope this explanation helps you understand the JavaScript microbenchmark being tested on MeasureThat.net!