var string = "Hello world!";
/Hello/.test(string);
string.includes("Hello");
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 | 45086504.0 Ops/sec |
String.includes | 163684736.0 Ops/sec |
String.match | 8903239.0 Ops/sec |
Let's break down the provided JSON data and explain what is tested in this JavaScript microbenchmark.
Benchmark Overview
The benchmark compares three different approaches to check if a specific string ("Hello") exists within another string:
test()
method.includes()
method.match()
method.Options Compared
The three options being compared are:
includes()
method, which checks if a substring exists within another string.match()
method, which returns an array of matches if a pattern exists in the string.Libraries and Special Features
String.includes
is a built-in method in JavaScript.Other Alternatives
Some alternative approaches to these methods include:
indexOf()
or lastIndexOf()
instead of includes()
.regex.exec()
instead of test()
.substring()
or other string manipulation methods to achieve similar results.Benchmark Preparation Code
The provided script preparation code is simple:
var string = "Hello world!";
This sets up a test string that will be used for all three benchmark cases.