from Hacker News

JS performance: try / catch versus checking for undefined

by ianox on 5/3/12, 9:03 AM with 26 comments

  • by judofyr on 5/3/12, 10:16 AM

    And? This isn't very surprising. Exceptions are slower than regular/shortjump control-flow.
  • by quink on 5/3/12, 12:00 PM

    Coming from the Python ecosystem, I am utterly horrified by those results. In fact, I thought the graphs were broken until I got to IE10.
  • by CJefferson on 5/3/12, 12:29 PM

    While perhaps not so extreme, I have found similar behaviour in C++. I worked on a code base where there was a really nice way of writing an algorithm which would 'throw' around 100,000 times/sec, and we had to change to something like this (keep checking return values of functions, and return if they are true), to get reasonable performance.
  • by underwater on 5/3/12, 5:14 PM

    Worth noting that `undefined` is a variable, and not a keyword.

       undefined = 1;
       >>> 1
       null = 1;
       >>> ReferenceError: invalid assignment left-hand side
    
    It looks like Chrome and Firefox no longer let you assign to window.undefined any, but I'm not sure is IE has the same restrictions. The correct way to check for undefined is via `typeof`.
  • by simondlr on 5/3/12, 9:59 AM

    Why is try/catch so slow?
  • by alexchamberlain on 5/3/12, 9:48 AM

    UI Feedback: It would be cool if we could collapse all the Chrome 19.x.x together.
  • by danssig on 5/3/12, 6:26 PM

    It doesn't matter. You should signal errors with exceptions, not by returning undefined no matter how much slower it is. After all, it's supposed to be an exceptional case.
  • by omgtehlion on 5/3/12, 1:36 PM

    Note IE10 results.
  • by lucb1e on 5/3/12, 10:57 AM

    Note that this is an old revision of the test.