Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes in string with check
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
Tests:
IndexOf
string.indexOf('tempor') > -1
Includes
string.includes('tempor') === true
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
206624160.0 Ops/sec
Includes
214766064.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **What is being tested?** The benchmark measures the performance difference between two string methods: 1. `string.indexOf('tempor') > -1` 2. `string.includes('tempor') === true` Both methods are used to search for a specific substring ("tempor") within a large string. **Options compared** The benchmark compares two approaches: 1. **`indexOf()`**: This method returns the index of the first occurrence of the specified value, or -1 if it's not found. It returns an integer value. 2. **`includes()`**: This method returns `true` if the string includes the specified value, and `false` otherwise. **Pros and Cons** * **`indexOf()`**: + Pros: Generally faster than `includes()` since it only needs to find a single character match (if any) and return an integer result. + Cons: Returns -1 when not found, which can be confusing for some developers. It also requires handling the possibility of no match being returned. * **`includes()`**: + Pros: More intuitive and easier to understand since it returns a boolean value directly. No need to worry about return types or handling no matches. + Cons: Generally slower than `indexOf()` since it needs to search for all occurrences, not just one. **Library and its purpose** In this benchmark, the `string` object is used, which is a built-in JavaScript object that provides various string methods. The `includes()` method is part of the ECMAScript standard and was introduced in ECMAScript 2015 (ES6). **Special JS feature or syntax** None are mentioned here. **Other alternatives** If you wanted to compare other approaches for searching within strings, you might consider: * Using a regular expression with the `test()` method instead of `indexOf()` or `includes()`. * Implementing your own string search algorithm using loops and comparisons. * Using a dedicated library like Lodash's `string.includes()` (not applicable here since it's built-in). In summary, this benchmark measures the performance difference between two commonly used string methods in JavaScript: `indexOf()` and `includes()`. The results can help developers optimize their code for better performance, especially when working with large strings.
Related benchmarks:
IndexOf vs Includes in string 333
IndexOf > -1 vs Includes in string
IndexOf vs Includes in string as boolean
IndexOf vs Includes in string 2
Comments
Confirm delete:
Do you really want to delete benchmark?