Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf boolean vs Includes on string
(version: 0)
if 50 elements array contains some word
Comparing performance of:
IndexOf vs Includes
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
Tests:
IndexOf
text.indexOf('printer') > -1
Includes
text.includes('printer')
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:
5 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0
Browser/OS:
Chrome 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
162670368.0 Ops/sec
Includes
166275504.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The benchmark is designed to compare two approaches: `indexOf` and `includes` methods for searching a string in a given text. The test case consists of an array with 50 elements, where one of the elements contains the word "printer". **What are we testing?** We're measuring the performance difference between two methods: 1. **`text.indexOf('printer') > -1`**: This method returns the index of the first occurrence of the string 'printer' in the text, or -1 if it's not found. 2. **`text.includes('printer')`**: This method returns a boolean value indicating whether the text includes the string 'printer'. **Options compared** The two methods have different performance characteristics: * `indexOf`: This method has to scan the entire string from left to right to find the match, which can be slower for large strings. * `includes`: This method uses a more efficient algorithm that checks if the string is present in the text using a hash table lookup. **Pros and Cons** **`indexOf`**: Pros: * Can handle edge cases (e.g., finding the last occurrence) easily. Cons: * Slower for large strings due to sequential scanning. **`includes`**: Pros: * Faster for large strings since it uses a hash table lookup. Cons: * May not be as efficient for very small strings or specific use cases where `indexOf` is more convenient. **Other considerations** The choice of method depends on the specific requirements of your application. If you need to frequently search for a specific word in a large text, `includes` might be a better option. However, if you need to find the index of an occurrence or handle edge cases, `indexOf` might be more suitable. **Library and special JS feature** Neither `indexOf` nor `includes` uses any external libraries. They are built-in methods of the JavaScript language. However, it's worth noting that modern JavaScript engines (like V8 in Chrome) often implement additional optimizations for string searches using techniques like " Boyer-Moore" or "Knuth-Morris-Pratt" algorithms. **Other alternatives** If you need to measure performance differences between other methods or approaches, some popular alternatives include: * Using `Array.prototype.indexOf()` instead of `String.prototype.indexOf()` * Comparing the performance of different string matching algorithms (e.g., regular expressions) * Measuring the impact of caching or memoization on benchmark results * Using a different data structure (e.g., arrays, linked lists) for the search Keep in mind that these alternatives might require modifications to the benchmark setup and testing procedure. I hope this explanation helps you understand what's being tested in MeasureThat.net!
Related benchmarks:
regex vs split with regex
String.split(regExp) vs String.split(string)
Regex collapse vs for loop
native replaceAll vs regex replace
Comments
Confirm delete:
Do you really want to delete benchmark?