Skip to content

Instantly share code, notes, and snippets.

@tusharmath
Last active November 6, 2016 03:27
Show Gist options
  • Save tusharmath/eaabea0bee3ad2e3b7ef16ca50f6ac14 to your computer and use it in GitHub Desktop.
Save tusharmath/eaabea0bee3ad2e3b7ef16ca50f6ac14 to your computer and use it in GitHub Desktop.
performance comparison of creating bound functions
const assert = require('assert')
console.log('NODE:', process.version)
console.log('V8:', process.versions.v8, '\n--------------')
class Bind {
constructor () {
this.value = 1000
this.update = this.__update.bind(this)
}
__update () {
this.value++
}
}
class Arrow {
constructor () {
this.value = 1000
this.update = () => this.__update()
}
__update () {
this.value++
}
}
function test (source) {
assert.strictEqual(source.value, 1000)
source.update.call(null)
assert.strictEqual(source.value, 1001)
}
function perf (func) {
const start = Date.now()
for (var i = 0; i < 10000000; ++i) {
func()
}
return Date.now() - start
}
function perfCtor (name, Ctor) {
const c = new Ctor()
const update = c.update
console.log(`${name}():new`, perf(function newTester () {
const update = new Ctor().update
update()
}))
console.log(`${name}:`, perf(function tester () {
update()
}))
}
test(new Bind())
test(new Arrow())
perfCtor('Arrow', Arrow)
perfCtor('Bind', Bind)
/* results
➜ temp node Javascript.js
NODE: v6.8.1
V8: 5.1.281.84
--------------
Arrow():new 3234
Arrow: 69
Bind():new 3211
Bind: 131
➜ temp nvm use 7
Now using node v7.0.0 (npm v3.10.8)
➜ temp node Javascript.js
NODE: v7.0.0
V8: 5.4.500.36
--------------
Arrow():new 3577
Arrow: 69
Bind():new 683
Bind: 138
*/
@tusharmath
Copy link
Author

Issue filed here.
https://t.co/Dgmu1HEQgQ

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