Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs RegExp vs indexof vs indexOf and substr
(version: 0)
Comparing performance of:
regEx vs RegExp vs indexof and substr vs indexOf
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /^test/; window.regexp = new RegExp(/^test/); window.match = 'test'; 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.regex; while (x < TOTAL_STRINGS) { const str = data[x]; regex.test(str); x += 1; }
RegExp
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regexp = window.regexp; while (x < TOTAL_STRINGS) { const str = data[x]; regexp.test(str); x += 1; }
indexof and substr
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]; const posAt = str.indexOf(match); str.substr(posAt, 1); 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; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
regEx
RegExp
indexof and substr
indexOf
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 benchmark test case. **Benchmark Definition** The benchmark is designed to compare the performance of four different approaches: 1. `regex`: using a regular expression (`^` anchor) to search for a pattern in strings. 2. `RegExp`: creating an instance of the `RegExp` class and using its `test()` method to search for a pattern in strings. 3. `indexOf` (with `substr`): using the `indexOf()` method to find the position of a substring, and then using `substr()` to extract a single character from the string at that position. 4. `indexOf`: using only the `indexOf()` method without `substr()`. **Options Compared** Each test case is comparing the performance of one specific approach (1, 2, or 3) against another (`indexof` with and without `substr`). The comparison is done within the same browser version and configuration. **Pros and Cons of Each Approach:** * `regex`: efficient for pattern matching, but can be slower due to regex engine overhead. Pros: flexible, widely supported; Cons: complex syntax, potential performance issues. * `RegExp`: more explicit than `regex`, but with similar performance characteristics. Pros: readable, maintainable code; Cons: verbose syntax, less flexible than native regex. * `indexOf` (with `substr`): using `substr()` can be slower due to string slicing overhead. Pros: simple, easy to understand; Cons: performance impact, requires two method calls. * `indexOf`: a single method call with no string slicing overhead. Pros: concise, efficient; Cons: may not work correctly if the pattern is not found. **Library Used** No external libraries are used in this benchmark. However, some built-in JavaScript methods are utilized: * `RegExp` class * `String.prototype.indexOf()` * `String.prototype.substr()`
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
Regex vs .indexOf vs .startsWith vs .indexof
Comments
Confirm delete:
Do you really want to delete benchmark?