Skip to content

Instantly share code, notes, and snippets.

@neonstalwart
Created June 13, 2011 14:38
Show Gist options
  • Save neonstalwart/1022882 to your computer and use it in GitHub Desktop.
Save neonstalwart/1022882 to your computer and use it in GitHub Desktop.
try/catch performance with requirejs
<!DOCTYPE html>
<html>
<head>
<script>
var testIterations = 1e5;
var benchmark = function(hint, m,i) {
var d1 = new Date, d2, r;
while(i--)
r = m();
d2 = new Date;
console.log(hint, d2.getTime()-d1.getTime());
}
var a = 0,
// simulate an AMD factory
factory = function () {
return {
inc: function () {
a += 1;
}
};
},
// simulate the module as a return value of the factory
module = null;
// define the module in a try/catch
try {
module = factory();
}
catch(e) {
}
benchmark( 'try-catch', function() {
// use the module
module.inc();
}, testIterations);
// reset all the test parameters
a = 0;
// define the module without a try/catch
module = factory();
benchmark( 'plain', function() {
// use the module
module.inc();
}, testIterations);
</script>
</head>
<body>
<p>
see console
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment