var string = "Hello world!";
var regex = /Hello/;
regex.test(string);
string.includes("Hello");
string.match("Hello");
string === "Hello"
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Regex.test | |
String.includes | |
String.match | |
=== |
Test name | Executions per second |
---|---|
Regex.test | 3972260.2 Ops/sec |
String.includes | 14075056.0 Ops/sec |
String.match | 2627883.5 Ops/sec |
=== | 14666133.0 Ops/sec |
I'll break down the benchmark and explain what's being tested, compared, and the pros and cons of each approach.
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark that tests four different approaches to check if a string contains or matches a specific substring:
==
(equality operator)String.includes()
String.match()
Regex.test()
(using regular expressions)Test Cases
Each test case consists of a single benchmark definition, which is a JavaScript expression that checks if the provided string contains or matches the specified substring.
regex.test(string);
: Tests the Regex.test()
function.string.includes("Hello");
: Tests the String.includes()
method.string.match("Hello");
: Tests the String.match()
method.string === "Hello"
; Tests the ==
operator.Library Used
In this benchmark, no specific JavaScript library is used apart from built-in methods like String.includes()
, String.match()
, and the Regex.test()
function itself.
Special JS Features or Syntax
There are a few special features and syntaxes present in these test cases:
===
operator (equality with strict equality) is being tested. This operator checks for both value and type equality.regex
) are used in the Regex.test()
function to match specific patterns.Comparison of Approaches
Here's a brief comparison of each approach, including their pros and cons:
includes()
for certain use cases.includes()
for simple substring searches.Alternative Approaches
Other alternative approaches could include:
String.prototype.indexOf()
instead of includes()
However, these alternatives might not be as efficient or widely supported as the methods tested in this benchmark.