1

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;
}
3
  • 1
    You could do some simple bench-marking yourself, putting them in a loop, and alternating between the two approaches Commented Oct 7, 2011 at 17:12
  • possible duplicate of How do you performance test JavaScript code? 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 commenter is the "most right": Try it and see! get data.) Commented Oct 7, 2011 at 17:13

5 Answers 5

4

It 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.

Sign up to request clarification or add additional context in comments.

Comments

2

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]) {
   ...
}

1 Comment

That's true. With arrays you could just use indexOf.
2

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.

Comments

2

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
  );
}

Comments

0

Use a map; faster yet. "Hundreds" of switches should never come close to passing a code review.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.