Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs endsWith vs indexOf vs lastIndexOf
(version: 0)
Comparing performance of:
Regex vs endswith vs indexof vs lastindexof
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexStart = /^test/; window.regexEnd = /test$/; window.match = 'test'; window.matchLength = window.match.length; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for( var i=0; i < len; i++ ) { text += possible.charAt(getRandomInt(possible.length)); } return text; } while (data.length < TOTAL_STRINGS) { data.push(makeRandomString(getRandomInt(20))); }
Tests:
Regex
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regexStart; while (x < TOTAL_STRINGS) { const str = data[x]; regex.test(str); x += 1; }
endswith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.endsWith(match) === 0; x += 1; }
indexof
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.indexOf(match) === 0; x += 1; }
lastindexof
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; var length = window.matchLength; while (x < TOTAL_STRINGS) { const str = data[x]; str.lastIndexOf(match) === str.length - length; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Regex
endswith
indexof
lastindexof
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
419.8 Ops/sec
endswith
1096.7 Ops/sec
indexof
11312.2 Ops/sec
lastindexof
296.6 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, compared, and their pros and cons. **Benchmark Overview** The provided benchmark compares four string manipulation methods in JavaScript: 1. `Regex` 2. `endsWith` 3. `indexOf` 4. `lastIndexOf` Each method is used to check if a given string (`str`) ends or starts with a specific pattern (`match`). **Test Case Analysis** Here's a brief analysis of each test case: * **Regex**: This test uses the `test()` method to search for the first occurrence of the regular expression in the input string. * **endsWith**: This test checks if the input string ends with the specified pattern using the `endsWith()` method. * **indexOf**: This test searches for the first occurrence of the specified pattern in the input string using the `indexOf()` method. * **lastIndexOf**: This test finds the last occurrence of the specified pattern in the input string using the `lastIndexOf()` method. **Library and Special JS Feature** The provided benchmark uses a custom library to generate random strings. The library is not explicitly named, but it's used to create an array of strings (`data`) with varying lengths. This allows for large-scale testing of each method. No special JavaScript features or syntax are mentioned in the test cases. However, note that some browsers may have specific optimizations or limitations when using certain string manipulation methods (e.g., `Regex`). **Comparison and Pros/Cons** Here's a brief comparison of the four methods: * **Regex**: Fast, but can be slow for large strings due to backtracking. + Pros: High performance, flexible pattern matching. + Cons: Can be complex, slower for very large strings. * **endsWith**: Simple and efficient for checking if a string ends with a specific pattern. + Pros: Fast, easy to use. + Cons: Only checks the end of the string; no support for regular expressions. * **indexOf**: Fast and efficient for searching the first occurrence of a pattern in a string. + Pros: High performance, straightforward usage. + Cons: May return -1 if the pattern is not found, can be slow for very large strings. * **lastIndexOf**: Similar to `indexOf`, but finds the last occurrence of a pattern instead. + Pros: Fast, easy to use. + Cons: May return -1 if the pattern is not found; slower than `indexOf` for very large strings. **Other Alternatives** If you were to implement these benchmarks yourself or explore alternative approaches: * **Using `String.prototype.includes()`**: Instead of implementing your own string manipulation methods, consider using the `includes()` method, which provides a similar functionality without the need for manual implementation. * **Using optimized string algorithms**: Depending on the specific use case, more efficient string algorithms like Boyer-Moore or Knuth-Morris-Pratt search may be available for certain platforms or libraries. Keep in mind that these alternatives might not provide exactly the same results as the original benchmark, but they can offer interesting trade-offs between performance, readability, and implementation complexity.
Related benchmarks:
Regex vs .indexOf vs .startsWith
Regex vs .indexOf vs .startsWith 2
Normalize path: JS Regex vs .endsWith vs .indexOf vs .slice
Regex vs .indexOf vs .includes
JavaScript: Regex vs .startsWith vs .indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?