Skip to content

Instantly share code, notes, and snippets.

@robpalme
Created September 7, 2017 07:58
Show Gist options
  • Save robpalme/0987f89b9f62342c95239a1989171788 to your computer and use it in GitHub Desktop.
Save robpalme/0987f89b9f62342c95239a1989171788 to your computer and use it in GitHub Desktop.
Creation & call times for different binding techniques
// 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.');
}
@robpalme
Copy link
Author

robpalme commented Sep 7, 2017

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.

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