var string = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsam aliquid architecto earum eveniet, commodi nesciunt officia enim, veritatis fugiat ea cumque ipsum, obcaecati deleniti necessitatibus magnam cupiditate consequatur numquam neque ducimus fugit aut nemo eligendi. Odit, cumque! Nisi nemo, tempore magni ad consequatur facere praesentium velit autem assumenda ipsum commodi aliquid nulla labore iste rerum blanditiis est sed enim aut obcaecati vitae. Sunt esse voluptates eaque facere tempora! Nemo corporis harum reprehenderit, placeat error incidunt, esse blanditiis doloremque accusantium adipisci alias fugiat. Aut et dolor ad, nihil obcaecati modi recusandae sed ipsa esse, placeat tempora hic, facilis nobis. Inventore, nemo.";
var regex = /architecto/;
regex.test(string);
string.includes("architecto");
string.match("architecto");
string.indexOf("architecto");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RegEx.test | |
String.includes | |
String.match | |
String.indexOf |
Test name | Executions per second |
---|---|
RegEx.test | 8693168.0 Ops/sec |
String.includes | 28768680.0 Ops/sec |
String.match | 6363811.5 Ops/sec |
String.indexOf | 24242802.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to compare the performance of three different string manipulation methods:
string.includes()
string.indexOf()
regex.test()
(using a regular expression)All tests are run on a long, randomly generated string that contains the substring "architecto".
String Manipulation Methods
Each test case uses one of these three methods to search for the substring:
string.includes(substring)
: This method checks if the string includes the specified substring.string.indexOf(substring)
: This method returns the index of the first occurrence of the substring in the string. If the substring is not found, it returns -1.regex.test(string)
: This method uses a regular expression to search for the pattern in the string. In this case, the pattern is "/architecto/".Library Used
The regex.test()
function uses the built-in JavaScript RegExp
object, which provides support for regular expressions in JavaScript.
Pros and Cons of Each Approach
string.includes()
: This method is simple and efficient but may not be suitable for searching for a specific pattern or substring.string.indexOf()
: This method is also fast and can provide accurate results but may not be suitable for searching for a specific pattern or substring.includes()
for some cases.regex.test()
: This method is more powerful and flexible but can be slower due to the overhead of compiling regular expressions.Other Considerations
string.replace()
, string.split()
, or string.split()
with a callback function.Alternatives
If you wanted to add more tests to this benchmark, you could consider adding tests for other string manipulation methods, such as:
string.replace()
: Tests replacing a substring with another value.string.split()
: Tests splitting a string into an array of substrings.string.match()
and string.exec()
: Tests using regular expressions with the match()
or exec()
methods.You could also consider testing different types of strings, such as:
Keep in mind that adding more tests can increase the complexity and duration of the benchmark.