var string = "Hello world!";
var regex = /Hello/;
regex.test(string);
string.includes("Hello");
string.match("Hello");
string.indexOf("Hello")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RegEx.test | |
String.includes | |
String.match | |
String.indexof |
Test name | Executions per second |
---|---|
RegEx.test | 9960911.0 Ops/sec |
String.includes | 13843043.0 Ops/sec |
String.match | 4720408.5 Ops/sec |
String.indexof | 508232384.0 Ops/sec |
Let's break down what's being tested in this benchmark and the options compared.
Benchmark Purpose: The purpose of this benchmark is to compare the performance of different string-related methods in JavaScript across various browsers and devices.
Script Preparation Code: The script preparation code defines two variables:
var string = "Hello world!";
var regex = /Hello/;
Here, string
is a simple string literal containing the text "Hello world!", and regex
is a regular expression pattern matching the substring "Hello".
Html Preparation Code: There is no HTML preparation code provided.
Test Cases:
test()
method for regular expressions on the defined string.includes()
method for searching for the substring "Hello" within the defined string.match()
method for finding the first occurrence of the substring "Hello" within the defined string.indexOf()
method for finding the index of the first occurrence of the substring "Hello" within the defined string.Comparison Options:
test()
methodincludes()
methodmatch()
methodindexOf()
methodPros and Cons of each approach:
test()
method:test()
function with a string as an argument.includes()
method:match()
method:g
flag (e.g., /Hello/g
). This can lead to faster performance compared to includes()
when dealing with large strings.indexOf()
method:Library and Purpose: None of the test cases explicitly use any external libraries.
Special JS features and syntax: There are no special JavaScript features or syntax mentioned in the benchmark definition.