Last active
October 7, 2015 15:29
-
-
Save moshen/35e440d2a2e81ca08464 to your computer and use it in GitHub Desktop.
binding this vs creating an anonymous 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
var Benchmark = require('benchmark'), | |
suite = new Benchmark.Suite; | |
var obj = { | |
name: 'myobj', | |
meth: function() { | |
return this.name; | |
} | |
}; | |
var anon = function() { | |
obj.meth(); | |
} | |
var bound = obj.meth.bind(obj); | |
suite.add('anon-function-in-iter', function() { | |
(function() { | |
obj.meth(); | |
})(); | |
}) | |
.add('bind-function-in-iter', function() { | |
obj.meth.bind(obj)(); | |
}) | |
suite.add('anon-function-once', function() { | |
anon(); | |
}) | |
.add('bind-function-once', function() { | |
bound(); | |
}) | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
.run(); |
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
{ | |
"name": "node-function-speed-tests", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"dependencies": { | |
"benchmark": "^1.0.0" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"start": "node index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "MIT" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Clone with:
git clone [email protected]:35e440d2a2e81ca08464.git node-function-speed-tests
My results: