Skip to content

Instantly share code, notes, and snippets.

@mifkata
Last active August 29, 2015 14:24
Show Gist options
  • Save mifkata/7fc4cd0aa32f33362f96 to your computer and use it in GitHub Desktop.
Save mifkata/7fc4cd0aa32f33362f96 to your computer and use it in GitHub Desktop.
A very basic logic vs regex speed test (tries to match min 8 char length, alphanumeric word, that contains at least 1 integer, 1 uppercase letter and 1 special character). Tests 10 mil words by default, modify "tests" value to change.
(function() {
var integers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
var upperChars = chars.map(function(i) { return i.toUpperCase(); });
var specials = ['!','@','#','$','%','^','&','*','+','='];
var regex = /^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&\*+=]).*$/;
var len, useint, usespecial, useupper, buffer;
var words = [];
var start = new Date().getTime();
var tests = 10000000;
// Generate buffer
console.log("Generating buffer of", tests, "test words");
for(var i = 0; i < tests; i++) {
useint = Math.ceil(Math.random()*10) > 4;
usespecial = Math.ceil(Math.random()*10) > 4;
useupper = Math.ceil(Math.random()*10) > 4;
len = Math.ceil(Math.random()*20);
buffer = '';
for(var m = 0; m < len; m++) {
if(useint) {
buffer += integers[Math.floor(Math.random()*integers.length)];
useint = false;
continue;
}
if(usespecial) {
buffer += specials[Math.floor(Math.random()*specials.length)];
usespecial = false;
continue;
}
if(useupper) {
buffer += upperChars[Math.floor(Math.random()*upperChars.length)];
useupper = false;
continue;
}
buffer += chars[Math.floor(Math.random()*chars.length)];
}
words.push(buffer);
}
console.log("Generated buffer in", calcTime(start), "milliseconds");
//
// Validate WORD BUFFER WITH FUNCTION
//
console.log("Starting to validate with string validator");
start = new Date().getTime();
var count = 0;
for(var i = 0; i < words.length; i++) {
count += validateStrings(words[i]) ? 1 : 0;
}
console.log("Completed with string validation function in", calcTime(start),"milliseconds",count,"strings are valid");
//
// Validate WORD BUFFER WITH REGEX
//
console.log("Starting to validate with regex");
start = new Date().getTime();
var count = 0;
for(var i = 0; i < words.length; i++) {
count += validateRegex(words[i]) ? 1 : 0;
}
console.log("Completed with string validation function in", calcTime(start),"milliseconds",count,"strings are valid");
/**
* uses programming logic to see if a string is valid,
* @param {[type]} str [description]
* @return {[type]} [description]
*/
function validateStrings(str)
{
var ints = false;
var specs = false;
var upper = false;
var c;
if(str.length < 8) {
return false;
}
for(var i = 0; i < str.length; i++) {
c = str[i];
if(!upper && upperChars.indexOf(c) > -1) {
upper = true;
continue;
}
if(!specs && specials.indexOf(c) > -1) {
specs = true;
continue;
}
if(!ints && integers.indexOf(parseInt(c)) > -1) {
ints = true;
continue;
}
if(chars.indexOf(c) === -1) {
return false;
}
}
return upper && specs && ints;
}
function validateRegex(str)
{
return regex.test(str);
}
function calcTime(fromTime)
{
return new Date().getTime() - fromTime;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment