Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Manual Search test
(version: 0)
Comparing performance of:
Manual Search vs Inverted Search
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
IndexOf
Script Preparation code:
var container = 'This is a long sample string' var substring = 'This is a long sample string';
Tests:
Manual Search
for (var containerIndex = 0; containerIndex < container.length; ++containerIndex) { var containerChar = container[containerIndex]; for (var substringIndex = 0; substringIndex < substring.length; ++substringIndex) { var i = (containerIndex + substringIndex) % substring.length; const substringChar = substring[i]; if (containerChar === substringChar) { } } }
Inverted Search
for (var substringIndex = 0; substringIndex < substring.length; ++substringIndex) { for (var substringLength = substring.length; substringLength > 0; --substringLength) { var match = container.indexOf(substring.substring(substringIndex, substringLength)); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Manual Search
Inverted Search
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):
Let's break down the benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark measures the performance of two approaches to find a substring within a larger string: manual search and `indexOf` (also known as inverted search). The test uses a sample string "This is a long sample string" and a variable-length substring "This is a long sample string". **Manual Search (Inverted Search)** The manual search approach involves iterating over the substring, reducing its length by one character at a time, and checking if the reduced substring exists in the container string using the `indexOf` method. This approach has to potentially check every possible substring of the original string. Pros: * Simple to implement * Can be optimized for certain edge cases Cons: * Has to iterate over all possible substrings, which can be slow for large strings * May not perform well for variable-length substrings or those with a high frequency of repeated characters **IndexOf (Inverted Search)** The `indexOf` method is used to find the index of the first occurrence of a substring within a string. This approach uses a more efficient algorithm that searches for the substring by iterating over the container string only once. Pros: * More efficient than manual search, especially for large strings * Reduces the number of potential matches to check Cons: * May not perform well if the substring is not found in the container string **Other Considerations** When using `indexOf`, it's worth noting that it returns -1 if the substring is not found, and the index of the first occurrence otherwise. This can affect performance depending on the specific use case. **Library Usage** In this benchmark, no external libraries are used besides `String.indexOf`. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax being tested in this benchmark. The test only uses standard JavaScript features like loops, variables, and string manipulation. **Alternatives** Other approaches to find a substring within a string include: 1. Boyer-Moore algorithm: An optimized algorithm that preprocesses the substring to reduce the number of comparisons needed. 2. Knuth-Morris-Pratt (KMP) algorithm: Another optimized algorithm that uses precomputation to speed up the search process. 3. Rabin-Karp algorithm: A hashing-based approach that can perform well for large strings and frequent searches. These algorithms are more complex to implement and might not be as straightforward to understand, but they offer better performance than manual search or `indexOf` for certain use cases.
Related benchmarks:
index vs lastindexof startsWith
substring vs indexOf
JavaScript search() vs indexOf()
String.indexOf vs String.indexOf with the second parameter
.includes() vs indexOf() for single-character search in string
Comments
Confirm delete:
Do you really want to delete benchmark?