I am currently using try/catch to handle JSON.parse errors. But I have just recently found out after reading some best practices that using try/catch on nodejs has some performance overhead. Is the performance overhead fixed already and is there another way to avoid errors on JSON.parse without using try/catch?
There is no simple way to avoid try-catch without your own parser.
Try this performance test: http://jsperf.com/try-catch-performance-overhead
Use Chrome for the test, since NodeJS is just V8. In my case there was a 41% performance penalty. However, unless you are going to reading thousands of JSONs per second, I suggest you just use the standard try-catch and make your code readable.
Note that for Firefox, there was virtually no difference between the 3 tests.
Best practice is to use try/catch at synchronous events:
try
{
dangerous.function();
}
catch(error);
{
console.log(error);
}
and a callback function for asynchronously fired events:
dangerous.function(function(error)
{
console.log(error);
});