Skip to content

Instantly share code, notes, and snippets.

@makenowjust
Created September 8, 2015 12:50
Show Gist options
  • Save makenowjust/85464857bfd66ebfdc22 to your computer and use it in GitHub Desktop.
Save makenowjust/85464857bfd66ebfdc22 to your computer and use it in GitHub Desktop.
benchmark Function#bind vs prototype
function X(x, y) {
this.x = x;
this.y = y;
if (process.env.BIND) {
this.n = this.n.bind(this);
}
}
X.prototype.n = function n() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
var
i, j, x,
iter = 1000000 * 1000;
console.time('loop');
x = new X(Math.random(), Math.random());
for (i = 0; i < iter; i++) {
x.n();
}
console.timeEnd('loop');
// env BIND=yes node bench-bind.js
// するとものすごく重くなる(いつまで経っても終わらないので計測できなかった)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment