Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .substr
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf vs .substr
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /^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; }
.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; }
.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]; if (str.substring(0,6) === 'test12') { console.log('test'); } x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Regex
.indexOf
.substr
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 is being tested, along with the pros and cons of each approach. **Benchmark Definition** The benchmark measures the performance of three different string manipulation approaches: 1. **Regex**: Using regular expressions to test if a given string contains a specific pattern. 2. **.indexOf**: Using the `indexOf()` method to search for a substring in a string. 3. **.substr**: Using the `substring()` method to extract a part of a string. **Script Preparation Code** The script preparation code sets up some global variables: * `window.regex`: a regular expression object that matches the pattern `/^test/`. * `window.match`: a constant string value `"test"`. * `window.data`: an array of 100,000 random strings generated using the `makeRandomString()` function. * `window.TOTAL_STRINGS`: the total number of strings in the `window.data` array. **Html Preparation Code** The html preparation code is empty and does not affect the benchmarking process. **Individual Test Cases** Each test case has a slightly different approach: 1. **Regex**: The script iterates over the `window.data` array, applies the `window.regex.test(str)` method to each string, and increments a counter. 2. **.indexOf**: The script iterates over the `window.data` array, uses the `str.indexOf(window.match) === 0` condition to test if the substring matches, and increments a counter. 3. **.substr**: The script iterates over the `window.data` array, checks if the first six characters of each string match `"test12"`, and prints "test" to the console if a match is found. **Pros and Cons** Here's a brief summary of each approach: * **Regex**: + Pros: Can handle complex pattern matching, fast for short strings. + Cons: Slow for large strings or complex patterns, may have performance issues with Unicode characters. * **.indexOf**: + Pros: Fast and efficient, handles Unicode characters well. + Cons: May not work correctly if the substring is at the end of the string, slower than regex for short strings. * **.substr**: + Pros: Simple and fast, suitable for fixed-length substrings. + Cons: Limited to fixed-length substrings, may not work correctly with Unicode characters. **Library Usage** In this benchmark, the `window.regex` object is a regular expression object created using the `/^test/` pattern. This library is built-in to JavaScript and provides a simple way to define regular expressions. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark. The script only uses standard JavaScript syntax and built-in libraries like `Math.random()` and `Array.prototype.forEach()`.
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?