var a = 'hello#a#bc#helloa';
var b = a.indexOf('helloa');
var a = 'hello#a#bc#helloa';
var b = a.includes('helloa');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Index Of | |
Includes |
Test name | Executions per second |
---|---|
Index Of | 162923632.0 Ops/sec |
Includes | 155269088.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Purpose
The main goal of this benchmark is to compare the performance of two string searching methods in JavaScript: indexOf
and includes
. These methods are used to find the index of a specific substring within a larger string.
Options Compared
Two options are compared:
true
if the value is found, and false
otherwise.Pros and Cons
indexOf()
:includes()
:indexOf()
.Other Considerations
The choice between these two methods depends on the specific use case. If you need to find a specific substring within a large string, indexOf()
might be faster. However, if you're searching for multiple values and want a simple, intuitive way to do so, includes()
is likely a better choice.
Library Used
None are explicitly mentioned in this benchmark.
Special JS Feature or Syntax
No special JavaScript features or syntaxes are used in these test cases.
Alternative Approaches
Other approaches to implement string searching include:
RegExp
object.