Javascript Try-Catch Performance Vs. Error Checking Code

前端 未结 7 1790
萌比男神i
萌比男神i 2020-11-28 09:56

Would it be faster to just put code inside a try-catch block instead of performing various error checks?

For example..

function getProjectTask(projec         


        
7条回答
  •  离开以前
    2020-11-28 10:25

    Performance wise try-catch is 20-50% slower than if checks (https://jsperf.com/throw-catch-vs-if-check/1).

    So, For rare usage, doesn't make much difference. For heavy usage, it might make some difference.

    However, I feel it's bad practice to use try-catch, if it can be done by if checks except if it greatly reduces readability.

提交回复
热议问题