Last active
April 27, 2022 11:25
-
-
Save bluefirex/1c412e9d7d77b9263747b8a5bfc7d3a4 to your computer and use it in GitHub Desktop.
RegEx vs. String Split vs. String Loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
== Results: == | |
1. Loop over string | |
2. Regex | |
3. .split() | |
== Output: == | |
Regex | |
59 | |
Array (split) | |
265 | |
Loop over String | |
23 | |
*/ | |
let microtime = () => Date.now() | |
let benchmark = (fn, ...args) => { | |
let start = microtime() | |
for (let i = 0; i < 100000; i++) { | |
fn(...args) | |
} | |
let end = microtime(), | |
diff = end - start | |
return diff | |
} | |
let isValidUIDRegex = uid => uid.length > 0 && uid.match(/^[^:*"\' ]+$/i) | |
let isValidUIDArray = uid => { | |
let chars = uid.split('') | |
for (let char of chars) { | |
if (char == ':' || char == '*' || char == '"' || char == '*' || char == '\'') { | |
return false | |
} | |
} | |
return true | |
} | |
let isValidUIDNative = uid => { | |
if (uid.length == 0) { | |
return false | |
} | |
for (let i = 0, max = uid.length; i < max; i++) { | |
let char = uid.charAt(i) | |
if (char == ':' || char == '*' || char == '"' || char == '*' || char == '\'') { | |
return false | |
} | |
} | |
return true | |
} | |
let cases = [ | |
'abc', | |
'a-b-c', | |
'a_b_c', | |
'a_b-c', | |
'ab.cd', | |
'ac/dc', | |
'this-is-vålid', | |
'høly-møly__this-is-v@lid', | |
'%432/43§', | |
'no spaces', | |
'don\'t_quote', | |
'no:guardchars', | |
'no*wildcards', | |
'' | |
] | |
console.log('Regex') | |
console.log(benchmark(_ => { | |
for (let c of cases) { | |
isValidUIDRegex(c) | |
} | |
})) | |
console.log('Array (split)') | |
console.log(benchmark(_ => { | |
for (let c of cases) { | |
isValidUIDArray(c) | |
} | |
})) | |
console.log('Loop over String') | |
console.log(benchmark(_ => { | |
for (let c of cases) { | |
isValidUIDNative(c) | |
} | |
})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
== Results: == | |
1. Regex | |
2. str_split | |
3. Loop over string | |
== Output: == | |
string(5) "Regex" | |
float(0.479864) | |
string(17) "Array (str_split)" | |
float(1.422509) | |
string(16) "Loop over string" | |
float(1.5971) | |
*/ | |
function benchmark(Callable $fn, ...$args) { | |
$start = microtime(true); | |
for ($i = 0; $i <= 100000; $i++) { | |
$fn(...$args); | |
} | |
$end = microtime(true); | |
$diff = ($end - $start); | |
return round($diff, 6); | |
} | |
function isValidUIDRegex(string $uid): bool { | |
return mb_strlen($uid) > 0 && preg_match('#^[^:*"\' ]+$#i', $uid); | |
} | |
function isValidUIDArray(string $uid): bool { | |
if (mb_strlen($uid) == 0) { | |
return false; | |
} | |
$chars = str_split($uid); | |
foreach ($chars as $char) { | |
if ($char == ':' || $char == ' ' || $char == '*' || $char == '\'' || $char == '"') { | |
return false; | |
} | |
} | |
return true; | |
} | |
function isValidUIDNative(string $uid): bool { | |
if (mb_strlen($uid) == 0) { | |
return false; | |
} | |
for ($i = 0, $max = mb_strlen($uid); $i < $max; $i++) { | |
$char = $uid[$i]; | |
if ($char == ':' || $char == ' ' || $char == '*' || $char == '\'' || $char == '"') { | |
return false; | |
} | |
} | |
return true; | |
} | |
$cases = [ | |
'abc', | |
'a-b-c', | |
'a_b_c', | |
'a_b-c', | |
'ab.cd', | |
'ac/dc', | |
'this-is-vålid', | |
'høly-møly__this-is-v@lid', | |
'%432/43§', | |
'no spaces', | |
'don\'t_quote', | |
'no:guardchars', | |
'no*wildcards', | |
'' | |
]; | |
var_dump( | |
'Regex', | |
benchmark(function() use ($cases) { | |
foreach ($cases as $case) { | |
isValidUIDRegex($case); | |
} | |
}), | |
'Array (str_split)', | |
benchmark(function() use($cases) { | |
foreach ($cases as $case) { | |
isValidUIDArray($case); | |
} | |
}), | |
'Loop over string', | |
benchmark(function() use($cases) { | |
foreach ($cases as $case) { | |
isValidUIDNative($case); | |
} | |
}), | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
== Result: == | |
1. .components(separatedBy: "") | |
2. Loop over string | |
3. Regex | |
== Output: == | |
Regex | |
5621 | |
Array (split) | |
1105 | |
Loop over string | |
3741 | |
*/ | |
import Foundation | |
func microtime() -> Int64 { | |
let date = Date() | |
return Int64((date.timeIntervalSince1970 * 1000.0).rounded()) | |
} | |
func benchmark(fn: () -> ()) -> Int64 { | |
let start = microtime() | |
for _ in 0..<100000 { | |
fn() | |
} | |
let end = microtime(), | |
diff = end - start | |
return diff | |
} | |
func isValidUIDRegex(_ uid: String) -> Bool { | |
if uid.count == 0 { | |
return false | |
} | |
let regex = try! NSRegularExpression(pattern: "^[^:*\"' ]+$"), | |
range = NSRange(location: 0, length: uid.utf16.count) | |
return regex.firstMatch(in: uid, options: [], range: range) != nil | |
} | |
func isValidUIDNative(_ uid: String) -> Bool { | |
if uid.count == 0 { | |
return false | |
} | |
for i in 0..<uid.count { | |
let char = uid[uid.index(uid.startIndex, offsetBy: i)] | |
if char == ":" || char == "*" || char == "\"" || char == "*" || char == "'" { | |
return false | |
} | |
} | |
return true | |
} | |
func isValidUIDArray(_ uid: String) -> Bool { | |
if uid.count == 0 { | |
return false | |
} | |
let chars = uid.components(separatedBy: "") | |
for char in chars { | |
if char == ":" || char == "*" || char == "\"" || char == "*" || char == "'" { | |
return false | |
} | |
} | |
return true | |
} | |
let cases = [ | |
"abc", | |
"a-b-c", | |
"a_b_c", | |
"a_b-c", | |
"ab.cd", | |
"ac/dc", | |
"this-is-vålid", | |
"høly-møly__this-is-v@lid", | |
"%432/43§", | |
"no spaces", | |
"don\'t_quote", | |
"no:guardchars", | |
"no*wildcards", | |
"" | |
] | |
print("Regex") | |
print(benchmark(fn: { | |
for c in cases { | |
isValidUIDRegex(c) | |
} | |
})) | |
print("Array (split)") | |
print(benchmark(fn: { | |
for c in cases { | |
isValidUIDArray(c) | |
} | |
})) | |
print("Loop over string") | |
print(benchmark(fn: { | |
for c in cases { | |
isValidUIDNative(c) | |
} | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment