Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String indexOf vs includes
(version: 0)
Comparing performance of:
indexOf vs includes
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = "penpineappleapplepen"
Tests:
indexOf
str.indexOf('pineapple') !== 1
includes
str.includes('pineapple')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexOf
includes
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 explain what's being tested. **Benchmark Overview** The benchmark is comparing the performance of two methods: `String.indexOf()` and `String.includes()`. Both methods are used to find the index of a substring within a string. **Options Compared** Two options are compared: 1. `String.indexOf()`: This method returns the index of the first occurrence of the specified value, or -1 if it's not found. 2. `String.includes()`: This method returns true if the string includes the specified value, and false otherwise. **Pros and Cons** * **`String.indexOf()`**: + Pros: Generally faster than `String.includes()` since it only needs to find the first occurrence of the substring. + Cons: Returns an index, which may not be immediately useful for all use cases. Also, it's more prone to errors if the string is modified or the substring is not found. * **`String.includes()`**: + Pros: Easier to understand and use, especially for developers who are new to JavaScript. It returns a boolean value (true/false), which can be more convenient than an index. + Cons: Generally slower than `String.indexOf()`, since it needs to check the entire string. **Library Used** In this benchmark, no specific library is used. The methods are part of the built-in JavaScript String prototype. **Special JS Feature or Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's commonly used in vanilla JavaScript. **Other Alternatives** If you need to measure the performance of other string-related methods, you might consider: * `String.prototype.replace()`: This method replaces all occurrences of a substring with another value. * `String.prototype.split()`: This method splits a string into an array of substrings based on a separator. For example, you could create a new benchmark that compares the performance of `String.indexOf()` and `String.includes()` against `String.prototype.replace()`, which might be useful if you need to perform regular expression replacements frequently.
Related benchmarks:
String indexOf vs startsWith/endsWith
IndexOf vs Includes str
Js Search -String IndexOf vs Includes
.includes() vs indexOf() for single-character search in string
Comments
Confirm delete:
Do you really want to delete benchmark?