Created
May 20, 2015 20:40
-
-
Save mraleph/4ba03cb374a20fc58dcb to your computer and use it in GitHub Desktop.
Strange performance affects from wrapping try/catch into a function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ../jsshell-35.0/js y0.js | |
// TryCatch x 2,139,927 ops/sec ±0.96% (65 runs sampled) | |
// FastTry x 2,079,778 ops/sec ±1.07% (63 runs sampled) | |
// Fastest is TryCatch | |
load("lodash.js"); | |
load("benchmark.js"); | |
function fastTry (fn) { | |
try { | |
return fn(); | |
} | |
catch (e) { | |
if (!(e instanceof Error)) { | |
return new Error(e); | |
} | |
else { | |
return e; | |
} | |
} | |
} | |
function factorial(op) { | |
// Lanczos Approximation of the Gamma Function | |
// As described in Numerical Recipes in C (2nd ed. Cambridge University Press, 1992) | |
var z = op + 1; | |
var p = [1.000000000190015, 76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 1.208650973866179E-3, -5.395239384953E-6]; | |
var d1 = Math.sqrt(2 * Math.PI) / z; | |
var d2 = p[0]; | |
for (var i = 1; i <= 6; ++i) { | |
d2 += p[i] / (z + i); | |
} | |
var d3 = Math.pow((z + 5.5), (z + 0.5)); | |
var d4 = Math.exp(-(z + 5.5)); | |
d = d1 * d2 * d3 * d4; | |
return d; | |
} | |
var suite = new Benchmark.Suite(); | |
suite | |
.add('TryCatch', function () { | |
try { | |
var d = 0; | |
d += factorial(10 * Math.random()); | |
d += factorial(2 * Math.random()); | |
return d; | |
} | |
catch (e) { | |
return e; | |
} | |
}) | |
.add('FastTry', function () { | |
return fastTry(function () { | |
var d = 0; | |
d += factorial(10 * Math.random()); | |
d += factorial(2 * Math.random()); | |
return d; | |
}); | |
}) | |
.on('cycle', function(event) { | |
print(String(event.target)); | |
}) | |
.on('complete', function() { | |
print('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
.run({'async': false}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ../jsshell-35.0/js y1.js | |
// TryCatch x 55,174 ops/sec ±3.26% (57 runs sampled) | |
// FastTry x 42,571 ops/sec ±4.10% (57 runs sampled) | |
// Fastest is TryCatch | |
load("lodash.js"); | |
load("benchmark.js"); | |
function fastTry (fn) { | |
try { | |
return fn(); | |
} | |
catch (e) { | |
if (!(e instanceof Error)) { | |
return new Error(e); | |
} | |
else { | |
return e; | |
} | |
} | |
} | |
(function () { | |
function factorial(op) { | |
// Lanczos Approximation of the Gamma Function | |
// As described in Numerical Recipes in C (2nd ed. Cambridge University Press, 1992) | |
var z = op + 1; | |
var p = [1.000000000190015, 76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 1.208650973866179E-3, -5.395239384953E-6]; | |
var d1 = Math.sqrt(2 * Math.PI) / z; | |
var d2 = p[0]; | |
for (var i = 1; i <= 6; ++i) { | |
d2 += p[i] / (z + i); | |
} | |
var d3 = Math.pow((z + 5.5), (z + 0.5)); | |
var d4 = Math.exp(-(z + 5.5)); | |
d = d1 * d2 * d3 * d4; | |
return d; | |
} | |
var suite = new Benchmark.Suite(); | |
suite | |
.add('TryCatch', function () { | |
try { | |
var d = 0; | |
d += factorial(10 * Math.random()); | |
d += factorial(2 * Math.random()); | |
return d; | |
} | |
catch (e) { | |
return e; | |
} | |
}) | |
.add('FastTry', function () { | |
return fastTry(function () { | |
var d = 0; | |
d += factorial(10 * Math.random()); | |
d += factorial(2 * Math.random()); | |
return d; | |
}); | |
}) | |
.on('cycle', function(event) { | |
print(String(event.target)); | |
}) | |
.on('complete', function() { | |
print('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
.run({'async': false}); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ../jsshell-35.0/js y2.js | |
// TryCatch x 54,241 ops/sec ±3.49% (58 runs sampled) | |
// FastTry x 2,092,810 ops/sec ±1.27% (62 runs sampled) | |
// Fastest is FastTry | |
load("lodash.js"); | |
load("benchmark.js"); | |
(function () { | |
function fastTry (fn) { | |
try { | |
return fn(); | |
} | |
catch (e) { | |
if (!(e instanceof Error)) { | |
return new Error(e); | |
} | |
else { | |
return e; | |
} | |
} | |
} | |
function factorial(op) { | |
// Lanczos Approximation of the Gamma Function | |
// As described in Numerical Recipes in C (2nd ed. Cambridge University Press, 1992) | |
var z = op + 1; | |
var p = [1.000000000190015, 76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 1.208650973866179E-3, -5.395239384953E-6]; | |
var d1 = Math.sqrt(2 * Math.PI) / z; | |
var d2 = p[0]; | |
for (var i = 1; i <= 6; ++i) { | |
d2 += p[i] / (z + i); | |
} | |
var d3 = Math.pow((z + 5.5), (z + 0.5)); | |
var d4 = Math.exp(-(z + 5.5)); | |
d = d1 * d2 * d3 * d4; | |
return d; | |
} | |
var suite = new Benchmark.Suite(); | |
suite | |
.add('TryCatch', function () { | |
try { | |
var d = 0; | |
d += factorial(10 * Math.random()); | |
d += factorial(2 * Math.random()); | |
return d; | |
} | |
catch (e) { | |
return e; | |
} | |
}) | |
.add('FastTry', function () { | |
return fastTry(function () { | |
var d = 0; | |
d += factorial(10 * Math.random()); | |
d += factorial(2 * Math.random()); | |
return d; | |
}); | |
}) | |
.on('cycle', function(event) { | |
print(String(event.target)); | |
}) | |
.on('complete', function() { | |
print('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
.run({'async': false}); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment