"abc".match(/abc/)
"abc".endsWith("abc")
"abc".includes("abc")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
match | |
endsWith | |
includes |
Test name | Executions per second |
---|---|
match | 36933980.0 Ops/sec |
endsWith | 90562848.0 Ops/sec |
includes | 228719712.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
What is being tested?
The test cases compare three different string manipulation functions in JavaScript:
String.prototype.match()
String.prototype.endsWith()
String.prototype.includes()
Each test case is designed to measure the performance of a specific function on a given input string: "abc"
.
Options being compared:
match
: This function searches for a pattern at the beginning of a string.endsWith
: This function checks if a string ends with a specified suffix.includes
: This function checks if a string includes a specified substring.Pros and Cons of each approach:
match
:endsWith
or includes
.endsWith
:match
. However, it can only search from the end of the string.includes
:match
, as it uses a simpler algorithm to check for substring presence. Also designed for common use cases like checking if a string contains a certain keyword or phrase.Library usage (if applicable):
In this benchmark, none of the test cases explicitly uses any libraries. However, it's worth noting that the String.prototype
methods mentioned (match
, endsWith
, and includes
) are part of the JavaScript language standard and do not require a library to be included in order to use them.
Special JS features or syntax:
The benchmark does not include any special JavaScript features or syntax beyond what is considered standard. However, it's essential to note that if you were to modify this test suite to include specific browser-specific features (e.g., let
, arrow functions, or class
declarations), the results might be affected by those differences.
Other alternatives:
If you wanted to compare these string manipulation functions with alternative approaches, you could consider using:
String.indexOf()
instead of match
, endsWith
, or includes
However, keep in mind that the current benchmark is well-suited for comparing the performance of match
, endsWith
, and includes
specifically.