Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes in string - larger string edition
(version: 0)
A benchmark of indexOf vs includes for searching strings in strings
Comparing performance of:
IndexOf vs Includes
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Iusto voluptatem minima tenetur molestiae, quaerat cum voluptates consectetur necessitatibus dignissimos doloremque repudiandae sequi! Deserunt aspernatur ratione maiores dicta, aut veniam sapiente.A, ipsum nulla commodi eius alias qui animi harum odio, quos veniam optio aliquam eos minima voluptatibus repellendus pariatur quam rerum sint sed ad nobis eveniet voluptates cumque nesciunt! Facere!Suscipit perspiciatis aperiam pariatur exercitationem quisquam esse! A reprehenderit asperiores veniam culpa, minus magni rerum tempora consequuntur repellendus sit qui molestiae adipisci fugiat error omnis animi iure eaque ipsum porro?Commodi, ipsam culpa. Deserunt dolorum accusamus est itaque iste sed molestiae perspiciatis nemo vitae debitis incidunt hic iure ea atque maiores non ipsum, accusantium sequi eum! Saepe odio eveniet minima?Suscipit, harum consequatur officiis pariatur fuga labore. Suscipit vitae aliquam, velit sint voluptatum maxime eos similique quaerat ullam, minima quod magni laudantium! Magnam voluptatibus sint deleniti, corporis delectus culpa a.Possimus odio quisquam consequatur ab blanditiis soluta, alias eius officia, veniam reprehenderit eum atque nesciunt vero id libero, ducimus aliquam iure! Cum sed error officia nam rerum velit, quod quis?Optio vitae nesciunt nemo vel illo iste in ea adipisci deserunt iure, modi, similique est, excepturi laudantium doloribus? Ipsam laborum numquam quo inventore impedit facilis maiores quae, itaque non nostrum.Ut odio nostrum magnam optio maxime magni, perferendis ab tenetur praesentium, architecto, officia beatae cumque provident aliquam deserunt fugiat quae ex quos quod rerum consequatur atque exercitationem? Enim, ad incidunt?Sit sapiente libero voluptatum, fugit vitae ex deleniti, qui illo accusamus incidunt explicabo ea autem nobis tempore, obcaecati aliquid? Pariatur corporis rerum cupiditate fugit laudantium mollitia, aspernatur possimus officia suscipit.Voluptate error fugit excepturi quod ipsa temporibus magnam nam hic. Quasi voluptas explicabo vel illum porro, dicta doloribus fugit ut atque quod, architecto magni distinctio, molestiae cupiditate nam. Cumque, ducimus?'
Tests:
IndexOf
string.indexOf('laudantium') !== -1
Includes
string.includes('laudantium')
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; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
1813788928.0 Ops/sec
Includes
1865466112.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. **Benchmark Definition** The benchmark is testing two different string searching methods: 1. `string.indexOf('laudantium') !== -1` 2. `string.includes('laudantium')` Both tests are searching for a specific substring ("laudantium") within a large string, but using different approaches: * `indexOf` method returns the index of the first occurrence of the specified value. If the value is not found, it returns -1. * `includes` method returns a boolean indicating whether the specified value exists in the string. **Options Compared** The benchmark is comparing two options: 1. **IndexOf**: Using the `indexOf` method to search for the substring. 2. **Includes**: Using the `includes` method to search for the substring. **Pros and Cons of Each Approach** * **IndexOf**: + Pros: Can be faster if you need to find a specific occurrence of a substring, as it returns an index value immediately. + Cons: May not be suitable if you're searching for multiple occurrences or want to check if a substring exists anywhere in the string without specifying an exact starting point. * **Includes**: + Pros: Returns a boolean indicating whether the substring exists in the entire string, which can be more convenient for some use cases. It also allows for more flexible searches (e.g., searching for multiple substrings). + Cons: May be slower than `indexOf` if you're only looking for a single occurrence, as it needs to scan the entire string. **Library and Syntax** The benchmark doesn't explicitly mention any libraries or syntax features that might affect its performance. However, it's worth noting that both `indexOf` and `includes` methods are part of the standard JavaScript API, so they should work in most environments without requiring additional dependencies. **Other Considerations** When choosing between `indexOf` and `includes`, consider the specific requirements of your use case: * If you need to find a single occurrence of a substring and want fast performance, `indexOf` might be the better choice. * If you need to search for multiple occurrences or want to check if a substring exists anywhere in the string without specifying an exact starting point, `includes` could be more suitable. **Alternatives** If you're looking for alternative benchmarking tools or methods, consider: 1. **Benchmarking libraries**: There are several JavaScript benchmarking libraries available, such as `benchmark` or `fast-benchmark`, which can help you create and run benchmarks. 2. **Profiling tools**: Tools like Chrome DevTools' Profiler or Firefox's Performance Monitor can provide detailed performance insights for your code. 3. **Custom benchmarks**: You can write custom benchmark scripts using JavaScript to test specific scenarios or functions. I hope this explanation helps clarify the purpose of the provided benchmark!
Related benchmarks:
slice vs substr vs substring (with end index & large string)
String Test indexOf vs Search
RegEx.test vs. String.includes vs. String.match vs String.indexOf (~2000 characters)
slice vs substring (long string)
Comments
Confirm delete:
Do you really want to delete benchmark?