I came to a point where I needed to check a condition and proceed with another condition if its true for many times.
Here I'm using fromNS
string which has the base of number (like - binary, decimal, octal) and then a condition to check if the
fromValue
is a valid number using regex.
If the value is invalid as per the base then displayError()
function is called.
from.addEventListener("input", function () {
fromValue = from.value;
// Is this method efficient
if((fromNS == "Binary" && !/^[01]*$/.test(fromValue)) ||
(fromNS == "Decimal" && !/^[0-9]*$/.test(fromValue)) ||
(fromNS == "Hexadecimal" && !/^[0-9a-fA-F]*$/.test(fromValue)) ||
(fromNS == "Octal" && !/^[0-7]*$/.test(fromValue)))
displayError();
// Or should I use this
switch (fromNS) {
case "Binary":
if(!/^[01]*$/.test(fromValue)) displayError();
break;
case "Decimal":
if(!/^[0-9]*$/.test(fromValue)) displayError();
break;
case "Hexadecimal":
if(!/^[0-9a-fA-F]*$/.test(fromValue)) displayError();
break;
case "Octal":
if(!/^[0-7]*$/.test(fromValue)) displayError();
break;
}
});
CodePudding user response:
I would go with one of these:
const conditions = [
formNS === "Binary" && !/^[01]*$/.test(form.value),
formNS === "Decimal" && !/^[0-9]*$/.test(form.value),
formNS === "Hexadecimal" && !/^[0-9a-fA-F]*$/.test(form.value),
formNS === "Octal" && !/^[0-7]*$/.test(form.value),
];
if (conditions.some(Boolean) {
displayError();
}
const conditions = [
{ ns: "Binary", regex: /^[01]*$/ },
{ ns: "Decimal", regex: /^[0-9]*$/ },
{ ns: "Hexadecimal", regex: /^[0-9a-fA-F]*$/ },
{ ns: "Octal", regex: /^[0-7]*$/ },
];
if (conditions.some(({ ns, value }) => formNS === ns && !value.match(regex))) {
displayError();
}
switch (true) {
case formNS === "Binary" && !/^[01]*$/.test(form.value):
case formNS === "Decimal" && !/^[0-9]*$/.test(form.value):
case formNS === "Hexadecimal" && !/^[0-9a-fA-F]*$/.test(form.value):
case formNS === "Octal" && !/^[0-7]*$/.test(form.value):
displayError();
}
CodePudding user response:
The switch statement is more efficient in cases where you need to compare many values from on variable.
The performance is even greater when used in many cases (eg: 10 - 15)
let val = 23;
// Goes through every if until it reaches the end
if (val > 40) {} // This line is checked
else if (val > 35) {/* ... */} // This line is checked
else if (val > 30) {/* ... */} // This line is checked
else if (val > 22) {/* do something! */} // This line is checked and executed
else if (val > 55) {/* ... */} // But this line is still checked
else if (val > 71) {/* ... */} // This is also checked
else {/* ... */}
Compare that to Switch/Case Statement that has break;
:
let val = 23;
switch(val) {
case val > 40:
// ...
break;
case val > 35:
// ...
break;
case val > 30:
// ...
break;
case val > 22:
// ...
break; // This block is executed and then the switch statement just ends here, no furthur checking is done
case val > 25:
// ...
break;
default:
// ...
break;
}
Basically the switch statement stops after the condition is met but the if statements continously checks the condition even if the line is executed.
But remember that the difference between if-else and switch statement is not much different, so the switch statement should only be used when a lot of values need to be checked, if-else works fine for 1-3 conditions!