Test name | Executions per second |
---|---|
Regex | 3944572.0 Ops/sec |
For loop, ParseInt | 1445041.2 Ops/sec |
For loop with switch case | 1440011.9 Ops/sec |
For loop, toString | 2588539.2 Ops/sec |
var dataPool = ["1234", "1X34", "0123", "12x4"];
var testStr = dataPool[Math.floor(Math.random() * dataPool.length)];
let isValid = true;
let regStr = `(${i+1}|X)`;
const regEx = new RegExp(regStr);
for (let i = 0; i < testStr.length; i++)
{
let code = testStr.charAt(i);
if(regEx.test(code) === false){
isValid = false;
break;
}
}
return isValid;
let isValid = true;
for (let i = 0; i < testStr.length; i++)
{
let code = testStr.charAt(i);
if (code === "X"){
}
else{
let codeValue = parseInt(code, 10);
if(codeValue !== i+1){
isValid = false;
break;
}
}
}
return isValid;
let isValid = true;
for (let i = 0; i < testStr.length; i++)
{
let char = testStr.charAt(i);
switch (true) {
case char === 'X':
break;
case parseInt(char, 10) === i + 1:
break;
default:
isValid = false;
}
}
return isValid;
let isValid = true;
for (let i = 0; i < testStr.length; i++)
{
let code = testStr.charAt(i);
if (code === "X"){
}
else{
if(code !== (i+1).toString()){
isValid = false;
break;
}
}
}
return isValid;