Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .substr vs includes
(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); 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 JSON and benchmark code to understand what is being tested. **Benchmark Purpose:** The benchmark tests the performance of three different string manipulation approaches: 1. Regular Expression (Regex) 2. `.indexOf` method 3. `.includes` method These methods are compared on a large dataset of randomly generated strings, which is created using the `makeRandomString` function. **Script Preparation Code:** This code sets up the environment for the benchmark. It defines several variables: * `window.regex`: A regular expression that matches any string starting with "test". * `window.match`: The string to be searched for in the data. * `window.data`: An array of randomly generated strings, which is populated with a total of 100,000 strings. * `window.TOTAL_STRINGS`: The total number of strings in the data. **Html Preparation Code:** This code is empty and doesn't contribute to the benchmark. **Individual Test Cases:** Each test case defines a single iteration of the benchmark. For example: * "Regex" uses a while loop to iterate over the data array, testing each string against the `window.regex` regular expression. * ".indexOf" uses a while loop to iterate over the data array, checking if the first occurrence of the `window.match` string is found in each string using the `.indexOf` method. * "includes" uses a while loop to iterate over the data array, checking if the `window.match` string is included in each string using the `.includes` method. **Library and Syntax:** The benchmark uses native JavaScript methods: * `.indexOf`: Returns the index of the first occurrence of the specified value. * `.includes`: Returns a boolean indicating whether the string includes the specified value. * Regular Expressions (`window.regex.test(str)`) are used to test if a string matches a pattern. **Other Considerations:** * The benchmark is likely designed to compare performance across different browsers, as indicated by the "Browser" and "DevicePlatform" fields in the latest benchmark results. * The use of random strings and a large dataset helps to minimize any biases or variations in performance between different browsers or implementations. * The benchmark may be used to evaluate the performance of JavaScript engines or browser implementations. **Alternatives:** Other alternatives for string manipulation that could be tested include: * `.startsWith`: Returns a boolean indicating whether the string starts with the specified value. * `.substr`: Returns a new string containing the substring starting at the specified index and having the specified length. * Other regular expression features, such as `match`, `replace`, or `split`. * String manipulation libraries or frameworks that provide optimized implementations of these methods.
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 Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Comments
Confirm delete:
Do you really want to delete benchmark?