Editing bind This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script> Array.slice = Array.from = function(array, start, end){ return Array.prototype.slice.call(array, start, end); }; Function.prototype.bind1 = function(bind){ var self = this, args = (arguments.length > 1) ? Array.slice(arguments, 1) : null; return function(){ if (!args && !arguments.length) return self.call(bind); if (args && arguments.length) return self.apply(bind, args.concat(Array.from(arguments))); return self.apply(bind, args || arguments); }; }; Function.prototype.bind2 = function(that){ var self = this, args = arguments.length > 1 ? Array.slice(arguments, 1) : null; var bound = function(){ var bind, F; if (this instanceof bound){ F = function(){}; F.prototype = self.prototype; bind = new F; } else bind = that; var result = (!args && !arguments.length) ? self.call(bind) : (args && arguments.length) ? self.apply(bind, args.concat(Array.from(arguments))) : self.apply(bind, args || arguments); return F ? bind : result; }; return bound; }; Function.prototype.bind3 = function(that){ var self = this, args = arguments.length > 1 ? Array.slice(arguments, 1) : null, F = function(){}; var bound = function(){ var context = that, length = arguments,length; if (this instanceof bound){ F.prototype = self.prototype; context = new F; } var result = (!args && !length) ? self.call(context) : self.apply(context, args && length ? args.concat(Array.slice(arguments)) : args || arguments); return context == that ? result : context; }; return bound; }; Function.prototype.bind4 = function(that){ var self = this, args = arguments.length > 1 ? Array.slice(arguments, 1) : null; var bound = function(){ var context = that, length = arguments,length; if (this instanceof bound){ var F = function(){}; F.prototype = self.prototype; context = new F; } var result = (!args && !length) ? self.call(context) : self.apply(context, args && length ? args.concat(Array.slice(arguments)) : args || arguments); return context == that ? result : context; }; return bound; }; (function(global){ var F = function(){}; Function.prototype.bind5 = function(that){ var self = this, args = arguments.length > 1 ? Array.slice(arguments, 1) : null; return function(){ var context = that, length = arguments,length; if (this && this != global){ F.prototype = self.prototype; context = new F; } var result = (!args && !length) ? self.call(context) : self.apply(context, args && length ? args.concat(Array.slice(arguments)) : args || arguments); return context == that ? result : context; }; }; })(this); Function.prototype.bind6 = function (bind) { var self = this; return function () { var args = Array.prototype.slice.call(arguments); return self.apply(bind || null, args); }; }; </script> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) var fn = function(type){ this.type = type; }; var obj = {type: 'bar'}; Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code var F = fn.bind(obj, 'foo', 'bar'); F(); Test 2 Title Async (check if this is an asynchronous test) Code var F = fn.bind1(obj, 'foo', 'bar'); F(); Test 3 Title Async (check if this is an asynchronous test) Code var F = fn.bind2(obj, 'foo', 'bar'); F(); Test 4 Title Async (check if this is an asynchronous test) Code var F = fn.bind3(obj, 'foo', 'bar'); F(); Test 5 Title Async (check if this is an asynchronous test) Code var F = fn.bind4(obj, 'foo', 'bar'); F(); Test 6 Title Async (check if this is an asynchronous test) Code var F = fn.bind5(obj, 'foo', 'bar'); F(); Test 7 Title Async (check if this is an asynchronous test) Code var F = fn.bind6(obj, 'foo', 'bar'); F();