Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String contains
(version: 0)
Comparing performance of:
IndexOf vs Includes
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str1 = '17.25'; var str2 = '1235'; var a; var b;
Tests:
IndexOf
a = str1.indexOf('.'); b = str2.indexOf('.');
Includes
a = str1.includes('.'); b = str2.includes('.');
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested:** The provided JSON represents a JavaScript microbenchmark, specifically testing the performance of two string methods: `indexOf` and `includes`. The benchmark measures how quickly these methods can find a specific character (`.`) within a given string. **Options compared:** Two options are being compared: 1. **`indexOf`**: This method returns the index of the first occurrence of the specified value, or -1 if it is not found. 2. **`includes`**: This method returns `true` if the string includes the specified value, and `false` otherwise. **Pros and Cons:** * **`indexOf`**: * Pros: * Faster execution time for large strings due to its optimized implementation. * Returns the index of the first occurrence, which can be useful in certain scenarios. * Cons: * May not always return the desired result (e.g., if the value is not found), as it only returns the index of the first match. * **`includes`**: * Pros: * Returns a boolean value, which can be more convenient in certain scenarios. * Less error-prone than `indexOf`, as it doesn't return an index that might be incorrect or out of bounds. * Cons: * Generally slower execution time compared to `indexOf` due to its non-optimized implementation. **Library and special JS feature:** Neither the provided benchmark nor the test cases explicitly use any libraries. However, it's worth noting that modern JavaScript implementations often rely on browser-specific optimizations or built-in functions like these string methods. No special JavaScript features are being tested in this example. **Other alternatives:** There are other methods to check if a string includes a specific value: * `toString().indexOf()`: This method works similarly to `indexOf`, but it returns the index of the first occurrence of the specified value within the entire string, including leading and trailing whitespace. * `String.prototype.includes()`: This is a more modern alternative to the previous methods. It's supported in most browsers and Node.js environments. Here's an example code snippet that compares these alternatives: ```javascript function testIncludes(needle) { var str = 'hello world'; // Basic includes console.log(str.includes(needle)); // Output: true // Using toString() and indexOf() console.log((str + '').toString().indexOf(needle)); // Output: 6 // Using String.prototype.includes() (modern browsers) if (typeof String.prototype.includes === 'function') { console.log(String(str).includes(needle)); } else { console.log('String.prototype.includes() is not supported.'); } } testIncludes('l'); ``` In conclusion, this benchmark provides a simple and straightforward way to compare the performance of two string methods: `indexOf` and `includes`. While there are alternative methods available, they may have different use cases or execution times.
Related benchmarks:
Number Comparison vs String Comparison addition
Extract number from string
Extract number from string - fixed
Number Comparison vs String Comparison addition 2
Comments
Confirm delete:
Do you really want to delete benchmark?