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
gemma2:9b
, generated one year ago):
This benchmark compares three different ways to check if the substring "tempor" exists within a long string: * **`string.indexOf('tempor') !== -1`:** This method uses the `indexOf()` function, which returns the index of the first occurrence of a substring. If the substring is not found, it returns -1. We compare the result to -1 to determine if the substring exists. * **`string.includes('tempor')`:** This method utilizes the `includes()` function, which directly checks if a substring is present within a string and returns true or false. * **`~string.indexOf('tempor')`:** This method uses bitwise negation (`~`) on the result of `indexOf()`. While it seems unconventional, some developers believe this can be faster in certain situations due to potential optimizations by the JavaScript engine. **Pros and Cons:** * **`indexOf()` + comparison:** * **Pro:** Widely known and commonly used. * **Con:** Can be slightly less efficient than `includes()`. * **`includes()`:** * **Pro:** More readable and concise, generally considered the most efficient method for this specific task. * **Con:** Introduced in ES6, so might not be supported in older JavaScript environments. * **Bitwise negation:** * **Pro:** Potentially faster due to engine optimizations (debatable). * **Con:** Highly unconventional and harder to read/understand. Performance gains are often negligible and can vary significantly depending on the engine and specific context. **Alternatives:** While these options are common, there are other ways to achieve a similar result: * **Regex:** A regular expression could be used for more complex pattern matching. * **Manual substring search:** For very simple cases, you could manually iterate through the string character by character. However, this is generally inefficient and not recommended. **Important Note:** Benchmark results can vary greatly depending on the browser, hardware, and other factors. It's crucial to consider multiple benchmark results from different sources and environments before making decisions based solely on performance.
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?