1

I have a code to generate math problems with random numbers. I am using switch statement to choose which task should be generated.

function genTask(taskid) {
    switch (taskid) {
        case 1:
        // generate some numbers
        return numbers;
        break;
        case 2:
        // generate some numbers
        return numbers;
        break;
        // ...
    }
}

I think there may be some performance issues when I add 150+ cases. Does this code go trough every case? Wouldnt it be faster if there are separate functions for every task?

function task1() {
    // generate some numbers
    return numbers;
}
function task2() {
    // ...
}

function genTask(taskid) {
    switch (taskid) {
        case 1:
        return task1();
        break;
        case 2:
        return task2();
        break;
        // ...
    }
}

Is there a faster way to do this?

2
  • No, it doesn't go through the code that is never executed. It surely does evaluate each of the case conditions, but that's the same in both of your snippets?! (Also it can be assumed that JS engines heavily optimise switching on primitive values against constant cases). Commented Aug 25, 2019 at 19:37
  • Do you really have 150 different cases? Don't they share some common functionality? Commented Aug 25, 2019 at 19:50

3 Answers 3

2

In general, I think the performance of "array" is better than "if/else" or "switch". See the reference below. In your specific case, if you comparing if/else to switch, then switch is better.

Using functions will not affect the performance ( I think ), but it is better and preferable as the code will be cleaner and readable.

Reference : https://www.oreilly.com/library/view/high-performance-javascript/9781449382308/ch04.html

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

5 Comments

please add current references that support this answer.
I think performance of "array" is better than "switch". Now who of us is right?
@Bergi, I added the reference I referred to. But I think it depends on many factors, and it worth checking more.
@J.K Ah, thanks, that's good stuff. (And validates my thought that array lookup is faster than switch for lots of cases - although the OP of course would need to benchmark his own code on his actual data to gain a valid comparison).
@Bergi, thank you for pointing this out. I think I have to edit my answer to avoid any confusion.
1

First of all, you need to know where you need to use if/else or switch/case.

When you need to check 2 to 3 conditions then you can use if/elseif/else

When you need to check 5 or above then definitely use switch/case

And based on speed switch/case is faster then if/else

Let's get back to the original point, In your case, you have 2 choices

  1. Write all code in one switch case.

  2. Make functions in chunk and call-in switch case.

I suggest you go with the second choice because switch case also a faster way of execution compared to other conditional checks, But when you make different functions then you can easily modify it and debug it which more help in development, and performance not compromised in that case.

2 Comments

The distinction between if/else and the conditional operator is not how many conditions there are, but whether you want a result value or not.
@Bergi Yes completely agree, But many of time people use ternary when they need single check and returns a result, By the way, I updated the answer
1

One more approach is you can use a object lookup

function task1() {
 // generate some numbers
 return numbers;
}
function task2() {
 // some task
}

const taskMap = { 1: task1, 2: task2 };
function genTask(taskid, defaultVal) {
 return (taskMap[taskid] && taskMap[taskid]()) || defaultVal;
}

This will be simple object lookup, though in terms of performance it might be slower than switch case but it increases resuability and readability of the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.