Created
April 15, 2013 20:54
-
-
Save azulus/5391218 to your computer and use it in GitHub Desktop.
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
// Tested in Node.js v0.10.2 | |
// * RESULTS * | |
// native bind with context x 696,362 ops/sec ±2.23% (84 runs sampled) | |
// native bind with context and arg x 672,228 ops/sec ±1.90% (86 runs sampled) | |
// custom bind with context x 1,186,009 ops/sec ±1.21% (98 runs sampled) | |
// custom bind with context and arg x 1,202,547 ops/sec ±1.28% (94 runs sampled) | |
// native bind invoke with 0 args and 0 curried args x 4,070,850 ops/sec ±0.81% (95 runs sampled) | |
// native bind invoke with 1 arg and 0 curried args x 3,666,720 ops/sec ±1.06% (97 runs sampled) | |
// native bind invoke with 0 args and 1 curried arg x 3,643,223 ops/sec ±1.22% (93 runs sampled) | |
// native bind invoke with 1 arg and 1 curried arg x 2,891,898 ops/sec ±0.86% (100 runs sampled) | |
// custom bind invoke with 0 args and 0 curried args x 15,214,179 ops/sec ±2.86% (90 runs sampled) | |
// custom bind invoke with 1 arg and 0 curried args x 6,188,506 ops/sec ±1.71% (95 runs sampled) | |
// custom bind invoke with 0 args and 1 curried arg x 9,247,504 ops/sec ±4.27% (86 runs sampled) | |
// custom bind invoke with 1 arg and 1 curried arg x 856,811 ops/sec ±2.51% (90 runs sampled) | |
// * TESTS * | |
var assert = require('assert') | |
var Benchmark = require('benchmark'); | |
var suite = new Benchmark.Suite; | |
var firstArg = 1; | |
var secondArg = 2; | |
function testFunc(arg1, arg2) {} | |
function wrap(f, context, var_args) { | |
var args = Array.prototype.slice.call(arguments, 2) | |
if (args.length) { | |
// may have to merge args | |
return function(var_args) { | |
var applyArgs = arguments.length ? args.concat(Array.prototype.slice.call(arguments, 0)) : args | |
return f.apply(context, applyArgs); | |
} | |
} else { | |
// no merging of args, just use the context + input args | |
return function(var_args) { | |
return context || arguments.length ? f.apply(context, arguments) : f() | |
} | |
} | |
} | |
var nativeBinds = { | |
withContext: testFunc.bind(null), | |
withContextAndArg: testFunc.bind(null, firstArg) | |
} | |
var customBinds = { | |
withContext: wrap(testFunc, null), | |
withContextAndArg: wrap(testFunc, null, firstArg) | |
} | |
// native binding tests | |
suite.add('native bind with context', function() { | |
testFunc.bind(null) | |
}) | |
.add('native bind with context and arg', function() { | |
testFunc.bind(null, firstArg) | |
}) | |
// custom binding tests | |
suite.add('custom bind with context', function() { | |
wrap(testFunc, null) | |
}) | |
.add('custom bind with context and arg', function() { | |
wrap(testFunc, null, firstArg) | |
}) | |
// custom bind invocation tests | |
.add('native bind invoke with 0 args and 0 curried args', function() { | |
nativeBinds.withContext() | |
}) | |
.add('native bind invoke with 1 arg and 0 curried args', function() { | |
nativeBinds.withContext(secondArg) | |
}) | |
.add('native bind invoke with 0 args and 1 curried arg', function() { | |
nativeBinds.withContextAndArg() | |
}) | |
.add('native bind invoke with 1 arg and 1 curried arg', function() { | |
nativeBinds.withContextAndArg(secondArg) | |
}) | |
// custom bind invocation tests | |
.add('custom bind invoke with 0 args and 0 curried args', function() { | |
customBinds.withContext() | |
}) | |
.add('custom bind invoke with 1 arg and 0 curried args', function() { | |
customBinds.withContext(secondArg) | |
}) | |
.add('custom bind invoke with 0 args and 1 curried arg', function() { | |
customBinds.withContextAndArg() | |
}) | |
.add('custom bind invoke with 1 arg and 1 curried arg', function() { | |
customBinds.withContextAndArg(secondArg) | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment