Question about switch

I recently completely a challenge (HTML Entities in Intermed. Algorithms) and, of course, there are various options. I found success with creating an object and establishing a key-value directory within it. As I had been perusing other options I came across the suggestion that switch could be used for this particular challenge as well. However, in writing it it seemed so cumbersome especially in relation to the elegance of creating a directory within an object and simply running regex below to select and replace particular elements.

My question is to professional programmers: do you use switch that often? I’ve heard its performance is not as efficient as other methods and it seems like one of these things that was good in early versions of JS but has now been usurped by new approaches available,

Are there still scenarios where you’ve found it useful? I say this because it’s fun to write but I realize it may just be a personal indulgence of mine.

Thank you for your feedback.

Switch is generally very efficient, though the syntax is not fantastic and it has several gotchas. It has quite specific use cases which limit its usage. It’s designed to check for things you can make an exact match on, then execute an option based on the match. This means if statements [generally] have to be used if there is conditional logic, and for dictionary lookup (like HTML entities) dictionary objects are obviously a more sensible choice.

But for particular usecases it’s more sensible than anything else - reducers in Redux for example. If:

  1. you want to switch on a single value and
  2. that value will always be the same type (switch case branches) and
  3. if it isn’t the same type, then that’ll just be bad input, and you can always handle that the same way (default branch of the switch) and
  4. in the branches you want to execute some arbitrary code.

Then switch is useful. 4 is important with respect to the HTML entities challenge, because that’s what differentiates it from object lookup. In the challenge, you just need to look up what a value is, so an object is correct. If you had to execute some code instead for each option, then possibly a switch would work well.

Minor thing, but note regex is [relatively] slow; if you can match without using it that’s generally always more efficient

1 Like

I would add one thing to @DanCouper’s analysis. I actually read an article advocating for object “lookup tables” like the one you designed, and it spurred my curiosity. Tests on jsperf (googleable, written by others), showed that in terms of speed object > switch > if/else.

  • Reasons to use object: May as well to default to it if your options are not redundant, and one key maps to one option. Cons: takes up memory, and you may end up repeating yourself if multiple keys maps to the same

  • Reasons to use switch: If you need to perform some actions in more than one case, and some actions for only one of those cases, e.g., if you need to brew and pour coffee, but just pour milk or OJ. Notice the lack of a break statement for the coffee case, which means that control flow of the program doens’t break there, it just keeps rolling through until the break statement below the “orange juice” case:

switch (beverage){
  case 'coffee':
    brew();
  case 'milk':
  case 'orange juice':
    pour();
  break;
  default:
  break;
}
  • Reasons to use if/else: Nested conditional logic, simple decisions with one or two outcomes, or complex conditional testing.
//nested conditionals:
if (n%2 === 0){
  console.log("even");
  if (n%3 === 0){
    console.log("divisible by 6");
  }
}

//simple decisions:
if (targetFound) shoot();
else scanArea();

//complex conditional testing
if (newNode.x === currentNode.x && newNode.y === currentNode.y){
  deleteNode(newNode);
}
1 Like