6 ms·
Interesting at first glance, but what is the performance of this? It looks like all cases would have to be evaluated before picking one, so even slower than an
by hacker_9 7y ago
Interesting at first glance, but what is the performance of this? It looks like all cases would have to be evaluated before picking one, so even slower than an if-else-if.
- XCSme 7y agoIsn't it going to short-circuit with the return of the first truthy statement?
- foldr 7y agoThe expression immediately to the right of each 'case' keyword is evaluated before the resulting values are compared to true.
- deleted 7y ago[deleted]
- richgieg 7y agoIf that's the case, then wouldn't both "asdf" and "qwer" be printed to the console when executing the following code? function test() { switch (true) { case console.log('asdf') === undefined: return 1; case console.log('qwer') === undefined: return 2; } } test();
- foldr 7y agoHmm, I wrote some similar test code before writing my comment, but only included one 'case'. Indeed, you're right, they're evaluated only as needed.