var a = ';
var a = ';
var a = ';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Starts With | |
Index Of | |
Includes |
Test name | Executions per second |
---|---|
Starts With | 55444500.0 Ops/sec |
Index Of | 171891120.0 Ops/sec |
Includes | 159626976.0 Ops/sec |
Overview
The provided benchmark measures the performance of three string search methods in JavaScript: startsWith
, indexOf
, and includes
. These methods are commonly used to check if a substring is present within a larger string.
Benchmark Definition
The JSON data defines two test cases:
Each test case involves creating a string (a
) with a sample Lorem ipsum text and then checking if another string (b
) starts with, contains as an index, or includes the specified substring.
Options Compared
The benchmark compares three different string search methods:
startsWith
: Returns true
if the string b
starts with the specified substring.indexOf
: Returns the index of the first occurrence of the specified substring in the string a
. If the substring is not found, returns -1
.includes
: Returns true
if the string a
includes the specified substring.Pros and Cons
startsWith
:indexOf
or includes
, especially when searching for a specific prefix.indexOf
:startsWith
, especially when searching for a specific prefix or substring in a large string.includes
:indexOf
, with fewer edge cases to consider. Suitable for most use cases where the search is exact.startsWith
due to the overhead of searching for the substring in the entire string.Library Usage
None of the benchmark test cases explicitly use a JavaScript library. However, it's worth noting that some libraries (e.g., Lodash) provide optimized implementations of these methods.
Special JS Features or Syntax
There are no special JavaScript features or syntax used in this benchmark.
Other Alternatives
Other string search methods in JavaScript include:
localeCompare
: Compares two strings based on their locale and language settings.match
: Returns an array of matches for a regular expression applied to the string.replace
: Replaces occurrences of a specified pattern in the string with a new value.Keep in mind that each of these methods has its own strengths, weaknesses, and use cases. This benchmark provides a concise way to compare the performance of three common string search methods: startsWith
, indexOf
, and includes
.
If you're interested in exploring other options or learning more about the trade-offs between these methods, I can provide additional resources or guidance!