Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .indexof
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf vs includes()
Created:
3 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; }
includes()
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.includes(match) === 0; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Regex
.indexOf
includes()
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 provided benchmark and explain what is being tested, compared options, pros and cons, library usage, special JavaScript features or syntax, and other considerations. **What is being tested:** The benchmark tests three different ways to perform string matching: 1. **Regex**: Uses a regular expression to test if a given string matches a specific pattern. 2. **.indexOf()**: Tests if the index of the first occurrence of a substring in another string is equal to 0. 3. **includes()**: Tests if a string includes another string. **Options compared:** Each option has its own strengths and weaknesses: * **Regex**: Pros: + Highly flexible and powerful for matching patterns. + Can be used for character classes, escaping, and grouping. + Cons: - Can be slower than other options due to complexity. - Requires proper escape sequences and syntax. * **.indexOf()**: Pros: + Fast and efficient for exact matches. + Easy to use and understand. + Cons: - Only returns the index of the first occurrence, not the actual match. - Fails if the substring is not found. * **includes()**: Pros: + Fast and efficient for substring matching. + Returns a boolean value indicating presence or absence. + Cons: - Can be slower than .indexOf() due to method call overhead. - Not all browsers support includes() in older versions. **Library usage:** There is no external library used in this benchmark. The only custom script provided creates an array of random strings and assigns a regular expression, substring, and matching function to global variables for testing purposes. **Special JavaScript features or syntax:** None are explicitly mentioned or utilized in the provided benchmark. However, it's worth noting that the use of `window` object and global variables might be specific to MeasureThat.net's environment. **Other considerations:** * **Test data**: The test data consists of an array of random strings with varying lengths. * **Performance**: Measuring performance is crucial in this context, as it provides insight into the efficiency of each matching method. * **Browser and OS variations**: The benchmark results are reported for different browsers (Chrome 107) and operating systems (Mac OS X 10.15.7), which helps to identify potential platform-specific differences. **Alternatives:** Other alternatives to test string matching could include: * Using a testing framework like Jest or Mocha with built-in string matching utilities. * Implementing custom string matching functions using regular expressions, substring methods, or other techniques. * Comparing the performance of different programming languages (e.g., JavaScript, Python, C++) when performing string matching.
Related benchmarks:
Regex vs .indexOf vs .startsWith
Regex vs .indexOf vs .startsWith 2
Regex vs .indexOf vs .includes
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Comments
Confirm delete:
Do you really want to delete benchmark?