Reduce context memoization when possible

This commit is contained in:
Justin Ridgewell
2017-06-03 23:52:04 -04:00
parent acdd3637bc
commit 9ce797dd34
3 changed files with 15 additions and 15 deletions

View File

@@ -29,16 +29,14 @@ export default function ({ types: t }) {
if (loose && atCall) {
// If we are using a loose transform (avoiding a Function#call) and we are at the call,
// we can avoid a needless memoize.
ref = chain;
check = ref;
check = ref = chain;
} else {
ref = scope.maybeGenerateMemoised(chain);
if (ref) {
check = t.assignmentExpression("=", ref, chain);
node[replaceKey] = ref;
} else {
ref = chain;
check = chain;
check = ref = chain;
}
}
@@ -53,10 +51,12 @@ export default function ({ types: t }) {
// Otherwise, we need to memoize the context object, and change the call into a Function#call.
// `a.?b.?()` translates roughly to `(_b = _a.b) != null && _b.call(_a)`
const { object } = chain;
const context = scope.generateUidIdentifierBasedOnNode(object);
scope.push({ id: context });
chain.object = t.assignmentExpression("=", context, object);
let context = scope.maybeGenerateMemoised(object);
if (context) {
chain.object = t.assignmentExpression("=", context, object);
} else {
context = object;
}
node.arguments.unshift(context);
node.callee = t.memberExpression(node.callee, t.identifier("call"));

View File

@@ -1,4 +1,4 @@
var _foo, _foo2, _foo$bar, _foo3, _foo4, _foo4$bar, _foo5;
var _foo, _foo2, _foo$bar, _foo3, _foo4, _foo4$bar;
(_foo = foo) == null ? void 0 : _foo(foo);
@@ -6,4 +6,4 @@ var _foo, _foo2, _foo$bar, _foo3, _foo4, _foo4$bar, _foo5;
(_foo$bar = (_foo3 = foo).bar) == null ? void 0 : _foo$bar.call(_foo3, foo.bar, false);
(_foo4 = foo) == null ? void 0 : (_foo4$bar = (_foo5 = _foo4).bar) == null ? void 0 : _foo4$bar.call(_foo5, foo.bar, true);
(_foo4 = foo) == null ? void 0 : (_foo4$bar = _foo4.bar) == null ? void 0 : _foo4$bar.call(_foo4, foo.bar, true);

View File

@@ -1,5 +1,5 @@
function test(foo) {
var _foo$bar, _foo$bar2, _foo, _foo$bar3, _foo2, _foo$bar4, _foo$bar5, _foo$bar6, _foo$bar6$baz, _foo$bar7, _foo$bar8, _foo$bar8$baz, _foo$bar9;
var _foo$bar, _foo$bar2, _foo$bar3, _foo$bar4, _foo$bar5, _foo$bar6, _foo$bar6$baz, _foo$bar7, _foo$bar7$baz;
foo == null ? void 0 : foo.bar;
@@ -9,15 +9,15 @@ function test(foo) {
foo == null ? void 0 : foo.bar();
(_foo$bar2 = (_foo = foo).bar) == null ? void 0 : _foo$bar2.call(_foo, foo.bar, false);
(_foo$bar2 = foo.bar) == null ? void 0 : _foo$bar2.call(foo, foo.bar, false);
foo == null ? void 0 : (_foo$bar3 = (_foo2 = foo).bar) == null ? void 0 : _foo$bar3.call(_foo2, foo.bar, true);
foo == null ? void 0 : (_foo$bar3 = foo.bar) == null ? void 0 : _foo$bar3.call(foo, foo.bar, true);
(_foo$bar4 = foo.bar) == null ? void 0 : _foo$bar4.baz(foo.bar, false);
foo == null ? void 0 : (_foo$bar5 = foo.bar) == null ? void 0 : _foo$bar5.baz(foo.bar, true);
(_foo$bar6 = foo.bar) == null ? void 0 : (_foo$bar6$baz = (_foo$bar7 = _foo$bar6).baz) == null ? void 0 : _foo$bar6$baz.call(_foo$bar7, foo.bar, false);
(_foo$bar6 = foo.bar) == null ? void 0 : (_foo$bar6$baz = _foo$bar6.baz) == null ? void 0 : _foo$bar6$baz.call(_foo$bar6, foo.bar, false);
foo == null ? void 0 : (_foo$bar8 = foo.bar) == null ? void 0 : (_foo$bar8$baz = (_foo$bar9 = _foo$bar8).baz) == null ? void 0 : _foo$bar8$baz.call(_foo$bar9, foo.bar, true);
foo == null ? void 0 : (_foo$bar7 = foo.bar) == null ? void 0 : (_foo$bar7$baz = _foo$bar7.baz) == null ? void 0 : _foo$bar7$baz.call(_foo$bar7, foo.bar, true);
}