Last Updated: June 19, 2022
·
1.661K
· prettydeveloper

Javascript: if vs. case performances

I was writing some javascript code and I was in doubt if it would have been better to use a bunch of if...else conditions or a more clean switch...case statement, so I ran a simple test on jsperf, and it turns out that the switch statement is 79% slower than the corresponding if construct!

Obviously it may depend by other factors in case of a more complex code, but still from now on I will prefer simple if statements when possible.

Picture

1 Response
Add your response

Nice and useful test!

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside