Performance of if-else, switch or map based conditioning

前端 未结 3 1755
Happy的楠姐
Happy的楠姐 2020-12-12 14:57

I was wondering about the performances of the following implementations of conditional structs in javascript.

Method 1:

 if(id===\         


        
3条回答
  •  再見小時候
    2020-12-12 15:27

    According to this JSBen.ch test, the switch setup is the fastest out of the provided methods (Firefox 8.0 and Chromium 15).

    Methods 3 and 4 are slightly less fast, but it's hardly noticeable. Clearly, the if-elseif method is significantly slower (FireFox 8.0).

    The same test in Chromium 15 does not show significant differences in performance between these methods. In fact, the if-elseif method seems to be the fastest method in Chrome.

    Update

    I have run the test cases again, with 10 additional entries. The hrefmap (methods 3 and 4) show a better performance.

    If you want to implement the compare method in a function, method 3 would definitely win: Store the map in a variable, and refer to this variable at a later point, instead of reconstructing it.

提交回复
热议问题