Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created August 26, 2016 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/29993cd91644a014704b54704a35f31d to your computer and use it in GitHub Desktop.
Save Raynos/29993cd91644a014704b54704a35f31d to your computer and use it in GitHub Desktop.
bind allocation performance

Node 6

raynos at raynos-ThinkPad-T440p  ~/projects/temp
$ node bind-perf.js 
v8 version:  5.0.71.60
.bind() allocate time: 311
fbind() allocate time: 51
raynos at raynos-ThinkPad-T440p  ~/projects/temp
$ node bind-perf.js 
v8 version:  5.0.71.60
.bind() allocate time: 307
fbind() allocate time: 48

node 4

raynos at raynos-ThinkPad-T440p  ~/projects/temp
$ node bind-perf.js 
v8 version:  5.0.71.60
.bind() allocate time: 311
fbind() allocate time: 51
raynos at raynos-ThinkPad-T440p  ~/projects/temp
$ node bind-perf.js 
v8 version:  5.0.71.60
.bind() allocate time: 307
fbind() allocate time: 48

node0.10

raynos at raynos-ThinkPad-T440p  ~/projects/temp
$ node bind-perf.js 
v8 version:  3.14.5.9
.bind() allocate time: 1122
fbind() allocate time: 53
raynos at raynos-ThinkPad-T440p  ~/projects/temp
$ node bind-perf.js 
v8 version:  3.14.5.9
.bind() allocate time: 1118
fbind() allocate time: 55
'use strict';
var WARMUP = 100;
var ALLOCATE_ITER = 1000 * 1000;
function MyClass() {
this.foo = 'some str';
this.bar = 42;
}
MyClass.prototype.getFoo = function getFoo() {
return this.foo;
}
function fbind(ctx, func) {
return function bound() {
return func.apply(ctx, arguments);
};
}
if (typeof process !== 'undefined') {
console.log('v8 version: ', process.versions.v8);
}
benchAllocateBind();
benchAllocateFBind();
// benchInvokeBind();
function benchAllocateBind() {
var obj = new MyClass();
var arr = [];
for (var i = 0; i < WARMUP; i++) {
arr[i % 10] = obj.getFoo.bind(obj);
}
var start = Date.now();
for (i = 0; i < ALLOCATE_ITER; i++) {
arr[i % 10] = obj.getFoo.bind(obj);
}
var end = Date.now();
console.log('.bind() allocate time: ' + (end - start));
}
function benchAllocateFBind() {
var obj = new MyClass();
var arr = [];
for (var i = 0; i < WARMUP; i++) {
arr[i % 10] = fbind(obj, obj.getFoo);
}
var start = Date.now();
for (i = 0; i < ALLOCATE_ITER; i++) {
arr[i % 10] = fbind(obj, obj.getFoo);
}
var end = Date.now();
console.log('fbind() allocate time: ' + (end - start));
}
@jdalton
Copy link

jdalton commented Aug 26, 2016

Edge 14Chrome 52Firefox 48


















Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment