jweinst1
9/3/2017 - 6:52 AM

jsperfregex.js

//performance test for regex start matching

//uses regex to check if string is a define
function isDefine(string) {
  if(string[0] === '@') {
    return /^[a-zA-Z_$][0-9a-zA-Z_$]*.*/.test(string.slice(1));
  } else return false;
}

console.time("regex and slice");
for(var i=0;i<1000000;i++) isDefine("@_$$$$$$372687264gddsfhjsgfjhdfsjhgewgfjhewgf");
console.timeEnd("regex and slice");

console.log(isDefine("@_$$$$$$372687264gdfhsjgdfsjhgewgfjhewgf"));

//regex and slice: 111ms