Created
September 7, 2017 07:58
-
-
Save robpalme/0987f89b9f62342c95239a1989171788 to your computer and use it in GitHub Desktop.
Creation & call times for different binding techniques
This file contains 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
// Micro-benchmark to answer the question in https://twitter.com/thejameskyle/status/905403367949647874 | |
if (typeof console === 'undefined') console = {log:print}; | |
var closuresOriginal = (function() { | |
const outer = a => { | |
const inner = b => a + b; | |
return inner(2); | |
}; | |
return function closuresOriginal(a) { | |
return outer(a); | |
} | |
}); | |
const inner1 = (a, b) => a + b; | |
var closuresInitialTransformHoisted = (function() { | |
const outer = a => inner1(a, 2); | |
return function closuresInitialTransformHoisted(a) { | |
return outer(a); | |
} | |
}); | |
var closuresInitialTransform = (function() { | |
const inner = (a, b) => a + b; | |
const outer = a => inner(a, 2); | |
return function closuresInitialTransform(a) { | |
return outer(a); | |
} | |
}); | |
var closuresSymbolTransform = (function() { | |
const _a = Symbol('a'); | |
const inner = b => inner[_a] + b; | |
const outer = a => { | |
inner[_a] = a; | |
return inner(2); | |
}; | |
return function closuresSymbolTransform(a) { | |
return outer(a); | |
} | |
}); | |
var closuresBind = (function() { | |
const inner = (a, b) => a + b; | |
const outer = inner.bind(undefined, 2); | |
return function closuresBind(a) { | |
return outer(a); | |
} | |
}); | |
const inner2 = (a, b) => a + b; | |
var closuresBindHoisted = (function() { | |
const outer = inner2.bind(undefined, 2); | |
return function closuresBindHoisted(a) { | |
return outer(a); | |
} | |
}); | |
var TESTS = [ | |
closuresOriginal, | |
closuresInitialTransform, | |
closuresInitialTransformHoisted, | |
closuresSymbolTransform, | |
closuresBind, | |
closuresBindHoisted, | |
]; | |
var a = 40; | |
var n = 2e7; | |
function test(fn) { | |
var result; | |
for (var i = 0; i < n; ++i) result = fn(a); | |
return result; | |
} | |
console.log('--- Create'); | |
for (var j = 0; j < TESTS.length; ++j) { | |
test(TESTS[j]); | |
} | |
for (var j = 0; j < TESTS.length; ++j) { | |
var startTime = Date.now(); | |
test(TESTS[j]); | |
console.log('Create ' + TESTS[j].name + ' : ', (Date.now() - startTime), 'ms.'); | |
} | |
console.log('--- Call'); | |
for (var j = 0; j < TESTS.length; ++j) { | |
test(TESTS[j]()); | |
} | |
for (var j = 0; j < TESTS.length; ++j) { | |
var startTime = Date.now(); | |
test(TESTS[j]()); | |
console.log('Call ' + TESTS[j].name + ' : ', (Date.now() - startTime), 'ms.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output from d8 on V8 master branch on MacBook.
--- Create
Create closuresOriginal : 664 ms.
Create closuresInitialTransform : 910 ms.
Create closuresInitialTransformHoisted : 669 ms.
Create closuresSymbolTransform : 2235 ms.
Create closuresBind : 1165 ms.
Create closuresBindHoisted : 677 ms.
--- Call
Call closuresOriginal : 308 ms.
Call closuresInitialTransform : 368 ms.
Call closuresInitialTransformHoisted : 248 ms.
Call closuresSymbolTransform : 788 ms.
Call closuresBind : 358 ms.
Call closuresBindHoisted : 372 ms.