Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf + strToLower
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf
Created:
5 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.toLowerCase()) === 0; 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):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros/cons. **Benchmark Overview** The benchmark compares two approaches to test if a string contains a certain substring: 1. **Regex (Regular Expressions)**: using the `test()` method with a regular expression. 2. **`.indexOf + str.toLowerCase()`**: using the `indexOf()` method followed by calling `toLowerCase()` on the matched substring. **Library Used** In this benchmark, the following libraries are used: * None explicitly mentioned, but we can assume that: + `window.regex` is an alias for a regex object (e.g., created with `new RegExp()`) or a library like jQuery's `regex` plugin. + `window.match` is likely a placeholder for the expected substring to test for. **Special JS Feature/Syntax** None mentioned explicitly, but it's worth noting that the use of template literals (`\r\nwindow.data = [];\r\nconst possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";`) and arrow functions (`function getRandomInt(max) { return Math.floor(Math.random() * max); }`) are modern JavaScript features. **Benchmark Test Cases** There are two test cases: 1. **Regex**: This test case uses the `test()` method with a regex object to check if any string in the data array matches the expected substring. 2. **.indexOf + str.toLowerCase()**: This test case uses the `indexOf()` method followed by calling `toLowerCase()` on the matched substring to check if the index of the first occurrence of the expected substring is 0. **Pros and Cons** **Regex (Test Case 1)** Pros: * Can be more flexible and powerful for matching complex patterns. * Can be faster for small datasets. Cons: * Can be slower for very large datasets due to the overhead of compiling a regex pattern. * May have performance issues with certain types of input data (e.g., Unicode characters). **.indexOf + str.toLowerCase() (Test Case 2)** Pros: * Faster execution time, especially for large datasets. * More straightforward and easier to understand. Cons: * Less flexible than regular expressions and may not match complex patterns. * May have performance issues if the input data is very large or contains many null characters. **Other Alternatives** For string matching, other approaches include: 1. **String.prototype.includes()**: a simpler alternative to `indexOf()` that returns true if the string includes the specified substring. 2. **String.prototype.match()**: can be used to match more complex patterns, but is slower than `indexOf()` and may not return any matches for cases where none exist. Overall, the choice of approach depends on the specific requirements and constraints of your use case. If you need a simple, fast way to test if a string contains a substring, `.indexOf + str.toLowerCase()` might be a good choice. However, if you need more flexibility or can tolerate slightly slower performance, regular expressions (using `test()`) could be a better fit.
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()
Regex /i vs .indexOf with tolowerCase once
Comments
Confirm delete:
Do you really want to delete benchmark?