Try/catch/finally is handled by TurboFan (one of V8's optimizing compilers) - see https://groups.google.com/forum/#!topic/v8-users/maH60gh_a8s for more detail. There is a certain set of features which go through TurboFan instead of CrankShaft (the other optimizing compiler).
Still, using `try` seems much slower (in Chrome 52):
var times = [[],[]]
var sums = [0, 0]
var funcs = [
function(){ sums[0]+=1 },
function(){try{ sums[1]+=1 }catch(e){}}
]
function bench(n){
var t0 = performance.now()
for(var i = 0; i< 30000; i++) funcs[n]()
times[n].push(performance.now()-t0)
}
for(var j = 0; j< 10000; j++) bench(j%2)
function avg(ary){return ary.reduce(function(acc, v){return acc+v}, 0)/ary.length}
console.log(times.map(avg))
// results:
// [bare , wrapped in try{} ]
// VM89:18 [0.347546000033617, 0.7464809999451041]
Let's do some more work inside the function / try block:
var times = [[],[]]
var sums = [0, 0]
var funcs = [
function(){ for (var i = 0; i < 1000; i++) sums[0]+=1 },
function(){try{ for (var i = 0; i < 1000; i++) sums[1]+=1 }catch(e){}}
]
function bench(n){
var t0 = performance.now()
for(var i = 0; i< 30; i++) funcs[n]()
times[n].push(performance.now()-t0)
}
for(var j = 0; j< 10000; j++) bench(j%2)
function avg(ary){return ary.reduce(function(acc, v){return acc+v}, 0)/ary.length}
console.log(times.map(avg))
// results:
// [bare , wrapped in try{} ]
// VM90:18 [0.09457900004982948, 0.5069960000008344]
I'm sure I'm doing many things wrong since I'm not a JS perf expert, but still, the difference here is goes from 2 to 50 times slower with the `try` block...
Edit: for the sake of completeness, on the same machine:
Edit: for the sake of completeness, on the same machine:
Firefox gives [10.2, 10.3] and [5.30, 5.45]
Safari gives [5.56, 5.60] and [4.91, 4.94]