alex wrote:wide_load wrote:are you sure about that ?
surely both methods perform the same logic ?

They are a faster alternative, yes.
test script
on my server this output
- If: 1.699187040329
- Switch: 2.085853099823
I know its not a very real world test, but it does suggest that switches are not always faster than if else if else ...
comparing the condition next to the case i.e. case (1 == 2) in a switch(true) block is the same as
which I think may be the reason for the slowness, its doing an extra comparison, the numbers back this up the switch is about 22% slower.
when used the way it is intended the speed difference is much smaller, but still the switch is slower.
test script
on the same server as before this output
- If: 1.6776959896088
- Switch: 1.9025719165802
again, i know this is not a real world example, but it does show that the switch is slower, and must be doing more comparison when used in the way (which i believe to be incorrect) you do in the video.
hopefully you will be able to explain this.