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.
Written by Lucia Moreno
Related protips
1 Response
Nice and useful test!
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Switch
Authors
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
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#