Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string.includes vs string.indexOf
(version: 0)
Comparing performance of:
includes vs indexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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;]"
Tests:
includes
a.includes("Mobile")
indexOf
a.indexOf("Mobile") != -1
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. `string.includes`: A method that returns `true` if the specified value exists in the string, or `false` otherwise. 2. `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`: + Pros: Generally faster than `indexOf`, especially for short strings. + Cons: May have a higher overhead due to its use of a binary search algorithm, which can be slower for very long strings. * `string.indexOf`: + Pros: Can be more accurate for finding exact matches, as it returns the index of the first occurrence rather than just confirming presence. + Cons: May be slower than `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.
Related benchmarks:
Js Search -String IndexOf vs Includes
Js Search - String StartsWith vs Includes
IndexOf vs Includes on string
indexOf vs includes 12
String.IndeOf vs. String.includes
Comments
Confirm delete:
Do you really want to delete benchmark?