Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf for charset
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /[=+-@\t\r]/; window.testChars = ['=', '+', '-', '@', '\t', '\r']; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789=+-@\t\r"; 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 testChars = window.testChars; while (x < TOTAL_STRINGS) { const str = data[x]; testChars.reduce((memo, testChar) => { if (memo !== -1) return memo; return str.indexOf(testChar); }, -1); x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
.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):
**Benchmark Overview** The provided benchmark, hosted on MeasureThat.net, compares the performance of two approaches to test for character presence in strings: regular expressions (`regex`) and the `String.indexOf()` method. **Test Case 1: Regex** In this test case, a regular expression is defined as `window.regex = /[=+-@\\t\\r]/`. This regex pattern matches any of the specified characters. The test then generates an array of random strings (`window.data`) with lengths between 1 and 20 characters, inclusive. The `while` loop iterates over each string in the data array, applies the regex pattern to each string using `regex.test(str)`, and increments a counter `x`. This process is repeated for `TOTAL_STRINGS` iterations. **Test Case 2: .indexOf** In this test case, a function is defined as `window.testChars = ['=', '+', '-', '@', '\\t', '\\r']`, which contains the same characters as the regex pattern. The test then generates an array of random strings (`window.data`) with lengths between 1 and 20 characters, inclusive. The `while` loop iterates over each string in the data array, applies the `String.indexOf()` method to each string using `testChars.reduce()`, which searches for the first occurrence of each character in the string. This process is repeated for `TOTAL_STRINGS` iterations. **Comparison and Pros/Cons** The two approaches have different performance characteristics: * **Regex (`regex`):** + Pros: - Can be more efficient for larger datasets, as it uses a single operation to test for multiple characters. - Can handle non-character data types (e.g., numbers) without modifications. + Cons: - May be slower for small datasets due to the overhead of creating and compiling the regex pattern. - May be less predictable for character sets with high cardinality. * **.indexOf (`testChars.reduce()`):** + Pros: - Faster for small datasets, as it uses a simple loop to iterate over each character. - More predictable performance, as the number of iterations is fixed and known. + Cons: - May be slower for larger datasets due to the overhead of creating and calling `String.indexOf()` multiple times. - Requires modifications to handle non-character data types. **Library Usage** In this benchmark, no external libraries are used. However, the `reduce()` method is a built-in JavaScript function that applies a callback function to each element in an array, reducing it to a single output value. **Special JS Features/Syntax** This benchmark does not explicitly use any special JavaScript features or syntax. However, it relies on modern JavaScript features such as: * `let` and `const` declarations (not shown in the code snippet) * Template literals (used for string concatenation) * Arrow functions (`=>`) * The `reduce()` method **Other Alternatives** If you need to compare performance between regular expressions and `String.indexOf()` in other scenarios, consider exploring these alternatives: * Using a different character set or regex pattern * Applying the test to different data types (e.g., numbers, dates) * Adding additional complexity to the test cases (e.g., nested loops, conditional statements) * Experimenting with different optimization techniques (e.g., caching, memoization)
Related benchmarks:
Regex vs .indexOf vs .startsWith 2
Regex vs .indexOf vs .includes
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Regex vs .indexOf vs .startsWith vs .indexof
Regex /i vs .indexOf with tolowerCase once
Comments
Confirm delete:
Do you really want to delete benchmark?