better object getter memoization - closes #353
This commit is contained in:
parent
961e0b9b6b
commit
46632e1a97
@ -1,4 +1,5 @@
|
||||
var traverse = require("../../traverse");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.Property =
|
||||
@ -13,27 +14,19 @@ exports.MethodDefinition = function (node, parent, file, scope) {
|
||||
var key = node.key;
|
||||
|
||||
if (t.isIdentifier(key) && !node.computed) {
|
||||
key = "_" + key.name;
|
||||
} else {
|
||||
key = file.generateUid("memo", scope);
|
||||
key = t.literal(key.name);
|
||||
}
|
||||
|
||||
var memoId = t.memberExpression(t.thisExpression(), t.identifier(key));
|
||||
var doneId = t.memberExpression(t.thisExpression(), t.identifier(key + "Done"));
|
||||
|
||||
traverse(value, {
|
||||
enter: function (node) {
|
||||
if (t.isFunction(node)) return;
|
||||
|
||||
if (t.isReturnStatement(node) && node.argument) {
|
||||
node.argument = t.assignmentExpression("=", memoId, node.argument);
|
||||
node.argument = t.memberExpression(util.template("object-getter-memoization", {
|
||||
KEY: key,
|
||||
VALUE: node.argument
|
||||
}), key, true);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// this._barDone = true;
|
||||
body.unshift(t.expressionStatement(t.assignmentExpression("=", doneId, t.literal(true))));
|
||||
|
||||
// if (this._barDone) return this._bar;
|
||||
body.unshift(t.ifStatement(doneId, t.returnStatement(memoId)));
|
||||
};
|
||||
|
||||
@ -10,9 +10,12 @@ var Foo = function Foo() {};
|
||||
_prototypeProperties(Foo, null, (function (_ref) {
|
||||
_ref[bar] = {
|
||||
get: function () {
|
||||
if (this._memoDone) return this._memo;
|
||||
this._memoDone = true;
|
||||
return this._memo = complex();
|
||||
return Object.defineProperty(this, bar, {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: complex()
|
||||
})[bar];
|
||||
},
|
||||
enumerable: true
|
||||
};
|
||||
@ -20,9 +23,12 @@ _prototypeProperties(Foo, null, (function (_ref) {
|
||||
})({
|
||||
bar: {
|
||||
get: function () {
|
||||
if (this._barDone) return this._bar;
|
||||
this._barDone = true;
|
||||
return this._bar = complex();
|
||||
return Object.defineProperty(this, "bar", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: complex()
|
||||
}).bar;
|
||||
},
|
||||
enumerable: true
|
||||
}
|
||||
@ -30,9 +36,12 @@ _prototypeProperties(Foo, null, (function (_ref) {
|
||||
|
||||
var foo = (function (_foo) {
|
||||
_foo[bar] = function () {
|
||||
if (this._memo2Done) return this._memo2;
|
||||
this._memo2Done = true;
|
||||
return this._memo2 = complex();
|
||||
return Object.defineProperty(this, bar, {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: complex()
|
||||
})[bar];
|
||||
};
|
||||
|
||||
return _foo;
|
||||
@ -40,9 +49,12 @@ var foo = (function (_foo) {
|
||||
Object.defineProperties(_ref2, {
|
||||
bar: {
|
||||
get: function () {
|
||||
if (this._barDone) return this._bar;
|
||||
this._barDone = true;
|
||||
return this._bar = complex();
|
||||
return Object.defineProperty(this, "bar", {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: complex()
|
||||
}).bar;
|
||||
},
|
||||
enumerable: true
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user