Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Search string engine benchmark
(version: 0)
Comparing performance of:
search vs indexOf vs includes vs test vs match vs boyerMooreHorspool
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
/** * Boyer Moor Horspool */ function boyerMooreHorspool(haystack, needle) { var badMatchTable = {}; var maxOffset = haystack.length - needle.length; var offset = 0; var last = needle.length - 1; var scan; if (last < 0) return 0; for (var i = 0; i < needle.length - 1; i++) { badMatchTable[needle[i]] = last - i; } while (offset <= maxOffset) { for (scan=last; needle[scan] === haystack[scan+offset]; scan--) { if (scan === 0) { return offset; } } offset += badMatchTable[haystack[offset + last]] || last || 1; } return -1; } function randomString() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } var str = randomString(); var findme = "a"; var findmeRegex = new RegExp(find);
Tests:
search
var result = str.search(findme);
indexOf
var result = str.indexOf(findme) > -1;
includes
var result = str.includes(findme);
test
var result = findmeRegex.test(str);
match
var result = str.match(findmeRegex);
boyerMooreHorspool
var result = boyerMooreHorspool(str, findmeRegex);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
search
indexOf
includes
test
match
boyerMooreHorspool
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition: Boyer-Moore Horspool Search String Engine** The benchmark tests various string searching algorithms in JavaScript, which is essential for many applications, including text processing, data retrieval, and more. The Boyer-Moore Horspool algorithm is a popular optimization of the classic linear search algorithm, known for its high performance. **Options Compared:** 1. **`str.includes(findme)`**: Uses the `includes()` method to check if a substring (`findme`) exists in the string (`str`). This method returns a boolean value indicating whether the substring is found. 2. **`findmeRegex.test(str)`**: Uses a regular expression (regex) to test if the string (`str`) matches the pattern defined by `findmeRegex`. The `test()` method returns a boolean value indicating whether the string matches the regex pattern. 3. **`str.match(findmeRegex)`**: Returns an array of matches for the regex pattern in the string (`str`). This method can return an empty array if no match is found. 4. **`str.search(findme)`**: Searches for the first occurrence of `findme` in the string (`str`) and returns its index (or -1 if not found). 5. **`boyerMooreHorspool(str, findmeRegex)`**: Implements the Boyer-Moore Horspool algorithm to search for `findme` in the string (`str`). The algorithm returns the starting index of the match or -1 if no match is found. **Pros and Cons:** * **`includes()`**: Simple and easy to understand, but may have performance issues with large strings. * **`test()` and `match()`**: Use regex patterns, which can be powerful but also complex to maintain. Performance may vary depending on the complexity of the pattern. * **`search()`**: Fast and efficient, but requires the string's length to be known in advance. * **`boyerMooreHorspool()`**: Optimized for performance, but more complex to understand and implement. **Other Considerations:** * The benchmark tests a short random string (`str`) generated by `randomString()`, which may not accurately represent real-world use cases. * The Boyer-Moore Horspool algorithm is an optimization of linear search, making it suitable for large strings. However, its performance may degrade if the input string has many repeated characters or patterns. **Library and Special JS Features:** The benchmark uses no external libraries besides the built-in `RegExp` class for regex testing. There are no special JS features mentioned in this benchmark. **Alternatives:** For similar benchmarks, consider using: * Micro-benchmarking frameworks like Jest or Mocha. * Online platforms like Google's Benchmarks or Apple's Performance Comparison Tool. * Real-world use cases and profiling tools to analyze performance differences between algorithms.
Related benchmarks:
Encode vs Blob
Compare TextEncoder, Blob, new TextEncoder
Blob vs TextEncoder
Performance Test: substring vs substr vs sliceyy
Comments
Confirm delete:
Do you really want to delete benchmark?