Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs Bitwise in string
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes vs Bitwise
Created:
5 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')
Bitwise
~string.indexOf('tempor')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
IndexOf
Includes
Bitwise
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.1:latest
, generated one year ago):
Let's break down what's being tested and compared in this benchmark. **The Benchmark:** The benchmark compares three different approaches to checking if a string contains a specific substring: 1. `indexOf()` 2. `includes()` 3. A bitwise operation using the `~` operator (which we'll get to later) **The Test Cases:** * `IndexOf`: This test case uses the `indexOf()` method, which returns the index of the first occurrence of the specified value (`"tempor"` in this case). If the value is not found, it returns `-1`. * `Includes`: This test case uses the `includes()` method, which returns a boolean indicating whether the string contains the specified value. * `Bitwise`: This test case uses a bitwise operation to check if the substring is present. We'll explain this in more detail later. **The Results:** The benchmark measures the execution speed (in executions per second) of each approach on a Chrome 113 browser running on Linux. Now, let's dive into what's being compared and some pros and cons of each approach: **IndexOf vs Includes:** * Both methods return a boolean indicating whether the string contains the substring. * `indexOf()` returns the index of the first occurrence if found, while `includes()` simply returns true or false. * In terms of performance, both methods are generally similar, with minor variations depending on the browser and environment. Pros: * Both methods are widely supported in JavaScript engines. * Easy to read and understand code-wise. Cons: * May have slight performance differences depending on the implementation. **Bitwise Operation (Bitwise):** * This test case uses a bitwise operation (~string.indexOf('tempor')). * The `~` operator is used to invert the result of `indexOf()`. If `indexOf()` returns -1 (indicating the substring was not found), the bitwise operation will return 0 (false). Otherwise, it will return a non-zero value (true). Pros: * Can be faster than other methods in some cases due to the nature of bitwise operations. Cons: * Not as widely supported as `indexOf()` and `includes()`, especially on older browsers. * Less readable code-wise, making it harder for others to understand. * May have edge cases where the performance gain is not significant. **Other Alternatives:** You can also use regular expressions or a simple loop-based approach to check if a string contains a substring. These alternatives may offer different trade-offs in terms of performance, readability, and browser support. For example: ```javascript function hasSubstring(str, substr) { for (let i = 0; i < str.length; i++) { if (str.substring(i, i + substr.length) === substr) return true; } return false; } ``` This approach is simple but less efficient than the others. Regular expressions can be more complex and may have their own performance characteristics. Keep in mind that the best approach will depend on your specific use case, target browsers, and performance requirements.
Related benchmarks:
IndexOf vs Includes in Larger string
IndexOf vs Includes vs Bitwise in string 2
Javascript: Case insensitive string comparison performance 3
IndexOf vs Includes in string - larger string edition
Comments
Confirm delete:
Do you really want to delete benchmark?