Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Try-Catch Performance Vs. Error Checking Code

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

For example..

function getProjectTask(projectTaskId) {     if (YAHOO.lang.isUndefined(projectTaskId) || YAHOO.lang.isNull(projectTaskId) && !YAHOO.lang.isNumber(projectTaskId)) {         return null;     }      var projectPhaseId, projectPhaseIndex, projectTaskIndex, projectPhases, projectPhase, projectTask;      if (!YAHOO.lang.hasOwnProperty(projectTaskPhaseMap, projectTaskId)) {         return null;     }      projectPhaseId = projectTaskPhaseMap[projectTaskId];      if (YAHOO.lang.isUndefined(projectPhaseId) || YAHOO.lang.isNull(projectPhaseId) || !YAHOO.lang.hasOwnProperty(scheduleData.ProjectPhasesMap, projectPhaseId)) {         return null;     }      projectPhaseIndex = scheduleData.ProjectPhasesMap[projectPhaseId];     if (YAHOO.lang.isUndefined(projectPhaseIndex) || YAHOO.lang.isNull(projectPhaseIndex) || !YAHOO.lang.hasOwnProperty(scheduleData.ProjectPhases[projectPhaseIndex])) {         return null;     }     projectPhase = scheduleData.ProjectPhases[projectPhaseIndex];      if (!YAHOO.lang.hasOwnProperty(projectPhase.ProjectTasksMap, projectTaskId)) {         return null;     }      projectTaskIndex = projectPhase.ProjectTasksMap[projectTaskId];      if (YAHOO.lang.isUndefined(projectTaskIndex) || YAHOO.lang.isNull(projectTaskIndex)) {         return null;     }      projectTask = scheduleData.ProjectTasks[projectTaskIndex]; } 

VS

function getProjectTask(projectTaskId) {     try {         projectPhaseId = projectTaskPhaseMap[projectTaskId];         projectPhaseIndex = scheduleData.ProjectPhasesMap[projectPhaseId];         projectPhase = scheduleData.ProjectPhases[projectPhaseIndex];         projectTaskIndex = projectPhase.ProjectTasksMap[projectTaskId];         projectTask = scheduleData.ProjectTasks[projectTaskIndex];      }     catch (e) {         return null;     } } 

I hope my question makes sense. I would be happy to clarify. Thank you!

like image 834
Abe Avatar asked Jul 10 '10 00:07

Abe


People also ask

Does Try Catch affect performance JavaScript?

As a conclusion, using try-catch in the case where error is never thrown seems to be as efficient as checking any simple condition. If the condition has anything more complex, try-catch is significantly faster.

Does Try Catch affect performance?

try/catch will only effect performance if an Exception is thrown (but that still isn't because of try/catch , it is because an Exception is being created). try/catch/finally does not add any additional overhead over try/catch .

Does try catch make code slower?

try catch block does not slow down your program at all and is basically a standard for catching exceptions. Try Catch statements is basically your safe net when it comes to bugs in your code/program.

Why you should not use try catch?

Without a try catch, you run the risk of encountering unhandled exceptions. Try catch statements aren't free in that they come with performance overhead. Like any language feature, try catches can be overused.


1 Answers

"Programs must be written for people to read, and only incidentally for machines to execute."

Abelson & Sussman, SICP, preface to the first edition

Always aim for readable code. The key thing to remember is:

Avoid try-catch in performance-critical functions, and loops

Anywhere else they won't do much harm. Use them wisely, use them when they make sense.

But as I see you clearly misuse some functions for error checking. You can test for the desired objects and properties of objects right before you use them instead of complex checking. And:

if (YAHOO.lang.isUndefined(projectPhaseId) || YAHOO.lang.isNull(projectPhaseId)) 

can be written as

if (projectPhaseId != null) 

for example... So the example above can be fairly readable even without try catches. You seem to misuse YUI a bit.

I would bet this works as expected:

function getProjectTask(projectTaskId) {     var projectPhaseId    = projectTaskPhaseMap[projectTaskId],        projectPhaseIndex = scheduleData.ProjectPhasesMap[projectPhaseId],        projectPhase      = scheduleData.ProjectPhases[projectPhaseIndex];    if (projectPhase == null) return null; // projectPhase would break the chain    var projectTaskIndex  = projectPhase.ProjectTasksMap[projectTaskId],       projectTask       = scheduleData.ProjectTasks[projectTaskIndex];     return projectTask || null; // end of the dependency chain  } 

How cool is that? :)

like image 142
25 revs, 4 users 83% Avatar answered Sep 21 '22 10:09

25 revs, 4 users 83%