var a = "Mozilla/5.0 (Linux; Android 10; moto g(7) power Build/QCOS30.85-18-6; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/86.0.4240.185 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/294.0.0.39.118;]"
a.includes("Mobile")
a.indexOf("Mobile") != -1
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
indexOf |
Test name | Executions per second |
---|---|
includes | 21954340.0 Ops/sec |
indexOf | 17510610.0 Ops/sec |
Let's break down the benchmark and its results.
Benchmark Definition
The benchmark is designed to test two methods of searching for a specific string within a given string:
string.includes
: A method that returns true
if the specified value exists in the string, or false
otherwise.string.indexOf
: A method that returns the index of the first occurrence of the specified value in the string, or -1
if not found.Options Compared
The two methods are being compared in terms of their performance, which is measured by the number of executions per second.
Pros and Cons of Each Approach
string.includes
:indexOf
, especially for short strings.string.indexOf
:includes
due to its more complex algorithm.Library and Its Purpose
In this benchmark, there is no specific library being used. The tests are written in plain JavaScript and utilize built-in methods of the string type.
Special JS Feature or Syntax
There are no special JS features or syntaxes being used in this benchmark that require explanation.
Other Alternatives
If you're interested in testing other string search algorithms, you could consider using libraries like:
String.prototype.search()
: This method returns the index of the first occurrence of a specified value within a string.String.prototype.lastIndexOf()
: This method returns the index of the last occurrence of a specified value within a string.Keep in mind that these alternatives may offer different trade-offs in terms of performance and accuracy compared to the built-in methods used in this benchmark.
Context
This benchmark is designed to help developers compare the performance of string.includes
and string.indexOf
, which are both commonly used methods for searching strings. By running this benchmark, you can gain insight into the relative efficiency of these two approaches on your specific hardware and software configuration.