In High Performance JavaScript, Nicholas Zakas talks about conditionals vs. if-else statements:
As it turns out, the switch statement is faster in most cases when compared to if-else, but significantly faster only when the number of conditions is large. The primary difference in performance between the two is that the incremental cost of an additional condition is larger for if-else than it is for switch. Therefore, our natural inclination to use if-else for a small number of conditions and a switch statement for a larger number of conditions is exactly the right advice when considering performance.
Generally speaking, if-else is best used when there are two discrete values or a few different ranges of values for which to test. When there are more than two discrete values for which to test, the switch statement is the most optimal choice.
Nice solution!
I was also contemplating a switch statement, but the computational overhead is allegedly very severe and simple if statements are preferable. Details: http://stackoverflow.com/a/12259830
Stackoverflow post referenced states that switch-immediate (which this is) is actually the fastest.
Also it's eight years old now and js uses a totally different optimizing compiler in 2020.
You shouldn't be using var.
That's all. Peace!
While it depends on the environment and the specific case, the safest bet for performance nowadays is switch over if-else.
https://jsperf.com/if-switch-lookup-table/113
This comment is hidden because it contains spoiler information about the solution
In High Performance JavaScript, Nicholas Zakas talks about conditionals vs. if-else statements:
https://www.safaribooksonline.com/library/view/high-performance-javascript/9781449382308/ch04s02.html
Why not use .every()
You cannot within
.map()
, as areturn
statement just exit the relevant function, in this case the one inside the.map()
method.If that is your goal, you might wish to use something like an ordinary loop, iterating over the array.
This comment is hidden because it contains spoiler information about the solution
Haven't met them before, thank you!
It's a ES6 feature, enabled via Babel.
Hello, what does the
=>
mean? Is it kind of an alternative function notation?That's great to hear!
Thank you, should have looked closer.
It can be found in your profile:
http://www.codewars.com/users/mark-outlast/comments
Nice solution!
I was also contemplating a switch statement, but the computational overhead is allegedly very severe and simple if statements are preferable. Details: http://stackoverflow.com/a/12259830
Loading more items...