const str = "Прицеп полуприцеп";
str.startsWith("Прицеп по");
const str = "Прицеп полуприцеп";
str.includes("полуприцеп");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
startsWith | |
includes |
Test name | Executions per second |
---|---|
startsWith | 70152592.0 Ops/sec |
includes | 34788616.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark is defined as follows:
startsWith
and includes
.Test Cases
"Прицеп полуприцеп"
starts with the substring "прицеп по"
. If it does, the benchmark measures the performance of the startsWith
method."Прицеп полуприцеп"
includes the substring "полуприцеп"
. If it does, the benchmark measures the performance of the includes
method.Options Compared
The benchmark compares two options:
Pros and Cons
includes
for certain cases, since it stops searching as soon as it finds the match.startsWith
, since it explicitly indicates that the substring is part of the original string.startsWith
for shorter substrings.Library and Special JS Features
There are no libraries used in this benchmark. However, the test cases use JavaScript syntax that is widely supported across different browsers.
Other Considerations
The benchmark measures the performance of these two methods on a specific string literal, which may not be representative of real-world usage patterns. In particular:
"Прицеп полуприцеп"
contains non-ASCII characters, which may affect the performance of certain browsers or libraries.Alternatives
There are other methods that could be used instead of startsWith
and includes
, such as:
startsWith
but slower than includes
.startsWith
and includes
but often slower.Overall, the benchmark provides a simple and concise way to compare the performance of two common string manipulation methods in JavaScript.