-
-
Save ibolmo/311145 to your computer and use it in GitHub Desktop.
bench suite for Function.prototype.bind
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
window.onload = function() { | |
var noop = function(){ return; } | |
,results = [] | |
,count = parseInt(location.hash.substr(1)) || 10000 | |
,sequence = (bench_sequence || 'bind_old bind_new bind_newer bind_even_newer bind_researched').split(' ') | |
,length = sequence.length | |
,i = 0 | |
,heading = document.getElementsByTagName('h1')[0].firstChild | |
,start | |
,name | |
,fn | |
,j | |
; | |
/* runs 2 tests | |
* - calls named fn with no args | |
* - calls named fn with 1 args | |
*/ | |
function test(name) { | |
var i = count | |
,index = name.indexOf('_args') | |
,fn = (index > -1 ? noop[name.substr(0, index)]({}, 1) : noop[name]({})) | |
,start = new Date | |
; | |
while (i--) { | |
fn() | |
} | |
results.push((new Date) - start); | |
i = count; | |
start = new Date; | |
while (i--) { | |
fn(1) | |
} | |
results.push((new Date) - start); | |
} | |
heading.data = 'Please wait. Benchmark in progress...'; | |
while (i < length) { | |
name = sequence[i++]; | |
test(name); | |
test(name + '_args'); | |
} | |
base = []; | |
baseArgs = []; | |
bound = []; | |
boundArgs = []; | |
start = ''; | |
function output(index, label, time) { | |
start += sequence[(index/4)|0] + ' (' + label + ')\t\t' + time + 'ms\n' + (index && ((++index % 4) === 0) ? '\n' : ''); | |
} | |
function worker(time, i) { | |
switch (i % 4) { | |
case 0: | |
base.push(time); | |
output(i, 'no args', time); | |
break; | |
case 1: | |
baseArgs.push(time); | |
output(i, 'with args', time) | |
break; | |
case 2: | |
bound.push(time); | |
output(i, 'bound; no args', time) | |
break; | |
case 3: | |
boundArgs.push(time); | |
output(i, 'bound with args', time) | |
} | |
} | |
for (i = 0; i < results.length; i++) { | |
worker(results[i], i); | |
} | |
document.getElementsByTagName('textarea')[0].value = start; | |
// max value of all results | |
i = Math.max.apply(null, results); | |
document.getElementsByTagName('img')[0].src = 'http://chart.apis.google.com/chart?cht=bhg&chtt=Bind+Performance+Comparison&chs=650x400&chbh=a,5,20&chdl=base|base%20called%20with%20args|bound%20args|bound%20args%20called%20with%20args&chco=FF0000,00FF00,0000FF,000000&chxt=x,t,y&chxl=2:|' + sequence.reverse().join('|') + '&chg=5,0&chd=t:' + base.join() + '|' + baseArgs.join() + '|' + bound.join() + '|' + boundArgs.join() + '&chds=0,' + i + '&chxr=0,0,' + i + '|1,0,' + i; | |
heading.data = 'Benchmark complete (' + count + ' iterations)'; | |
}; |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>binding benchmarks</title> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js"></script> | |
<script type="text/javascript" src="http://gist.github.com/raw/311145/a3b8b8a2c2245bde3d0858486a9521cbc9368e21/bench.bind.js"></script> | |
<script type="text/javascript"> | |
Function.prototype.bind_old = function() { | |
if (arguments.length < 2 && Object.isUndefined(arguments[0])) { return this; } | |
var __method = this, args = $A(arguments), object = args.shift(); | |
return function () { | |
return __method.apply(object, args.concat($A(arguments))); | |
}; | |
} | |
Function.prototype.bind_new = function () { | |
if (arguments.length < 2 && Object.isUndefined(arguments[0])) { return this; } | |
var __method = this, args = $A(arguments), object = args.shift(); | |
if (args.length) { | |
return function () { | |
return __method.apply(object, args.concat($A(arguments))); | |
}; | |
} | |
return function () { | |
return __method.apply(object, arguments); | |
}; | |
} | |
Function.prototype.bind_newer = (function(){ | |
var _slice = Array.prototype.slice; | |
return function(context) { | |
var fn = this, | |
args = _slice.call(arguments, 1); | |
if (args.length) { | |
return function() { | |
return arguments.length | |
? fn.apply(context, args.concat(_slice.call(arguments))) | |
: fn.apply(context, args); | |
} | |
} | |
return function() { | |
return arguments.length | |
? fn.apply(context, arguments) | |
: fn.call(context); | |
}; | |
} | |
})(); | |
Function.prototype.bind_even_newer = function(context) { | |
var fn = this, args = Array.prototype.slice.call(arguments, 1); | |
return function(){ | |
return fn.apply(context, Array.prototype.concat.apply(args, arguments)); | |
}; | |
}; | |
Function.prototype.bind_researched = (function(slice, concat) { | |
return function(context) { | |
var fn = this, args = slice.call(arguments, 1); | |
if (args.length) { | |
return function() { | |
return arguments.length | |
? fn.apply(context, concat.apply(args, arguments)) | |
: fn.apply(context, args); | |
} | |
} | |
return function() { | |
return arguments.length | |
? fn.apply(context, arguments) | |
: fn.call(context); | |
}; | |
} | |
})(Array.prototype.slice, Array.prototype.concat); | |
Function.prototype.bind_fixed_even_newer = function(context) { | |
var fn = this, args = Array.prototype.slice.call(arguments, 1); | |
return function(){ | |
if (args.length) Array.prototype.unshift.apply(arguments, args); | |
return arguments.length ? fn.apply(context, arguments) : fn.call(context); | |
}; | |
}; | |
bench_sequence = 'bind_old bind_new bind_newer bind_even_newer bind_fixed_even_newer bind_researched'; | |
</script> | |
</head> | |
<body> | |
<h1>test not run (javascript required)</h1> | |
<textarea cols="60" rows="20"></textarea> | |
<img/> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment