I have these two functions and I want to know which one is faster. I assume the first one, but what if I have hundreds of cases to evaluate?
function isSpecialKey(k) {
switch (k) {
case 9:
return true;
break;
case 16:
return true;
break;
case 17:
return true;
break;
case 18:
return true;
break;
case 20:
return true;
break;
case 37:
return true;
break;
case 38:
return true;
break;
case 39:
return true;
break;
case 40:
return true;
break;
default:
return false;
break;
}
}
function isSpecialKey(k) {
var arr = [9, 16, 17, 16, 8, 20, 37, 38, 39, 40]
for (i = 0; i < arr.length; i++) { if (k == arr[i]) { return true; } }
return false;
}
I have these two functions and I want to know which one is faster. I assume the first one, but what if I have hundreds of cases to evaluate?
function isSpecialKey(k) {
switch (k) {
case 9:
return true;
break;
case 16:
return true;
break;
case 17:
return true;
break;
case 18:
return true;
break;
case 20:
return true;
break;
case 37:
return true;
break;
case 38:
return true;
break;
case 39:
return true;
break;
case 40:
return true;
break;
default:
return false;
break;
}
}
function isSpecialKey(k) {
var arr = [9, 16, 17, 16, 8, 20, 37, 38, 39, 40]
for (i = 0; i < arr.length; i++) { if (k == arr[i]) { return true; } }
return false;
}
Share
Improve this question
edited Oct 7, 2011 at 17:12
Thomas Clayson
29.9k26 gold badges152 silver badges226 bronze badges
asked Oct 7, 2011 at 17:09
Puerto AGPPuerto AGP
4236 silver badges11 bronze badges
3
- 1 You could do some simple bench-marking yourself, putting them in a loop, and alternating between the two approaches – Dexygen Commented Oct 7, 2011 at 17:12
- possible duplicate of How do you performance test JavaScript code? – No Results Found Commented Oct 7, 2011 at 17:12
- Another variation would be var specialKeyBools = [0,0,0,0,0,0,0,,0,1,1,0,0,0,0,0,0,1,...]; function isSpecialKey(k) { return sepcialKeyBools[k]; } Also speeds up to put your array outside the function so it only happens once. (But the other menter is the "most right": Try it and see! get data.) – david van brink Commented Oct 7, 2011 at 17:13
5 Answers
Reset to default 4It is very unlikely to matter, not even with hundreds of cases. It might start mattering with thousands or tens of thousands but in that case, you probably shouldn't be using JavaScript anyway! At least not in a web browser.
Generally - the second way is the only way that makes sense from a maintenance perspective. I would absolutely take that.
However, for your specific case, there is a better solution.
Instead of doing this, just create a map:
var specialKeys = {
9: true,
16: true,
17: true,
...
40: true
};
Then you can just test it like so:
if(specialKeys[value]) {
...
}
How about?
function isSpecialKey(key) {
return [9, 16, 17, 16, 8, 20, 37, 38, 39, 40].indexOf(key) > -1;
}
Update. I've just remembered there are some browsers that don't support indexOf
on arrays, but I forgot which of them, so be careful.
You can use fallthrough in the switch, which makes for a lot less code:
function isSpecialKey(k) {
switch (k) {
case 9:
case 16:
case 17:
case 18:
case 20:
case 37:
case 38:
case 39:
case 40:
return true;
}
return false;
}
Consider also:
function isSpecialKey(k) {
return (
k == 9 ||
k == 16 ||
k == 17 ||
k == 18 ||
k == 20 ||
k == 37 ||
k == 38 ||
k == 39 ||
k == 40
);
}
Use a map; faster yet. "Hundreds" of switches should never e close to passing a code review.