I am somewhat new to the whole js / jQuery thang but is there any way to test javascript / jQuery code for efficiency? Because whether i run this as a loop, if stack, switch case, on vs live vs keydown etc, Im not sure if im trying to make this more difficult on myself.
Specifically referring to javascript without libraries: I think you should probably write it however you feel it is easier to read. The performance differences between the various methods are so small you wouldn't notice it most of the time. Also, you may find for statements are faster than whiles in certain situations or in different browsers, etc.
Do whatever is nicest for you. I think you'll pick up efficiency tips as you go along. Best not to get too caught up with that now.
ternaryorshorthandmethod for this. Maybe even a switch case?I'm sure there is an even more efficient way than this method (maybe using a for in loop), but this is what I came up with (demo):
@Mottie
I am totally going to check this out now.
I am somewhat new to the whole js / jQuery thang but is there any way to test javascript / jQuery code for efficiency? Because whether i run this as a loop, if stack, switch case, on vs live vs keydown etc, Im not sure if im trying to make this more difficult on myself.
Specifically referring to javascript without libraries: I think you should probably write it however you feel it is easier to read. The performance differences between the various methods are so small you wouldn't notice it most of the time. Also, you may find
forstatements are faster thanwhiles in certain situations or in different browsers, etc.Do whatever is nicest for you. I think you'll pick up efficiency tips as you go along. Best not to get too caught up with that now.
I just realized the thing that is missing in the code... it needs a filter to allow alphabetic keys through and a return false at the end (demo).