Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Search words/regex in string: matchAll vs split
(version: 1)
Comparing performance of:
regex vs includes
Created:
11 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
function generateRandomText(length) { const chars = 'abcdefghijklmnopqrstuvwxyz0123456789., '; let result = ''; for (let i = 0; i < length; i++) { const randomIndex = Math.floor(Math.random() * chars.length); result += chars[randomIndex]; } return result; } const test = generateRandomText(1000) const literalSearch = ["tg", "reg", "ha", "yo","mnasiwk"].sort( (a, b) => b.length - a.length ); const regSearch = ["c\\d", "a.{3}a", "<.+>", "^g.?a$"].sort( (a, b) => b.length - a.length ); const inc = regSearch.map(r => new RegExp(r)).concat(literalSearch); const regArr = regSearch.concat(literalSearch) const reg = new RegExp("(" + regArr.join(")|(") + ")", "g"); function regexMethod(str) { console.log(reg.source, str) const res = {} for (const match of str.matchAll(reg)) { for (let i = 1; i <= regArr.length; i++) { if (!!match[i]) { res[regArr[i-1]] = (res[regArr[i-1]] ?? 0) + 1 break; } } } return res } function includeMethod(strArr) { const res = {} let nextArr = [] for (const sep of inc) { for (const str of strArr) { const parts = str.split(sep); if (parts.length !== 1) res[sep.source ?? sep] = (res[sep.source ?? sep] ?? 0) + parts.length - 1 nextArr.push(...parts) } strArr = nextArr; nextArr = []; } return res }
Tests:
regex
regexMethod(test)
includes
includeMethod([test])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regex
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!
Comments
Confirm delete:
Do you really want to delete benchmark?