Created
October 4, 2010 05:05
-
-
Save uhop/609265 to your computer and use it in GitHub Desktop.
Calling 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
// Benchmarking different ways to call a function. | |
var context = {}, fb = null; | |
function test(){} | |
this.group( | |
"Calling function", | |
function Normal(x){ return test(x); }, | |
{ | |
name: "Function.call()", | |
test: function(x){ return test.call(context, x); } | |
}, | |
{ | |
name: "Function.apply()", | |
test: function(x){ return test.apply(context, arguments); } | |
}, | |
{ | |
name: "bind()", | |
setup: function(){ fb = bind(test, context); }, | |
test: function(x){ fb(x); } | |
}, | |
{ | |
name: "Function.bind()", | |
setup: function(){ fb = test.bind(context); }, | |
test: function(x){ fb(x); } | |
} | |
); | |
// utilities | |
function bind(f, o){ | |
return function(){ return f.apply(o, arguments); }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Benchmark it with http://www.perfjs.com/