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 | 8888432.0 Ops/sec |
String.includes | 21061762.0 Ops/sec |
String.match | 5044408.0 Ops/sec |
String.indexOf | 21047628.0 Ops/sec |
Let's dive into the benchmark.
What is being tested?
The provided JSON represents a microbenchmark that compares the performance of three different methods to test if a specific string exists within another string:
String.includes()
String.indexOf()
RegEx.test()
(using a regular expression)These methods are compared in four individual test cases, each with its own unique characteristics.
Options being compared
Here's a brief overview of each option and their pros and cons:
String.includes()
: This method returns true
if the specified value is present within the string, and false
otherwise.String.indexOf()
: This method returns the index of the first occurrence of the specified value in the string, or -1 if not found.RegEx.test()
: This method returns true
if the regular expression pattern is matched at the beginning of the string (the start of the string).String.includes()
and String.indexOf()
, suitable for complex patterns and edge cases.Library usage
In this benchmark, the RegEx
library is used to create a regular expression pattern (/Hello/
). This library provides an API for working with regular expressions in JavaScript.
Special JS features or syntax
There are no special JavaScript features or syntaxes being tested in this benchmark. The focus is solely on comparing the performance of three different string comparison methods.
Other alternatives
If you're interested in exploring alternative string comparison methods, here are a few options:
String.prototype.startsWith()
: This method returns true
if the string starts with the specified value.String.prototype.endsWith()
: This method returns true
if the string ends with the specified value.Array.prototype.includes()
: This method is similar to String.includes()
, but applies to arrays instead of strings.These alternatives might be suitable for specific use cases, but their performance characteristics may vary compared to the methods being tested in this benchmark.