Skip to content

Commit

Permalink
optimize: skip memoising right for ArrayPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Mar 29, 2022
1 parent dc3d28d commit 4eb651d
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 63 deletions.
13 changes: 12 additions & 1 deletion packages/babel-plugin-proposal-destructuring-private/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ type Item = {
restExcludingKeys?: ExcludingKey[] | null;
};

function rightWillBeReferencedOnce(left: LHS) {
switch (left.type) {
// Skip memoising the right when left is an identifier or
// an array pattern
case "Identifier":
case "ArrayPattern":
return true;
default:
return false;
}
}
/**
* Transform private destructuring. It returns a generator
* which yields a pair of transformed LHS and RHS, which can form VariableDeclaration or
Expand Down Expand Up @@ -342,7 +353,7 @@ export function* transformPrivateKeyDestructuring(
(index = indexPath.shift()) !== undefined ||
left.type === "AssignmentPattern"
) {
if (!scope.isStatic(right) && left.type !== "Identifier") {
if (!rightWillBeReferencedOnce(left) && !scope.isStatic(right)) {
const tempId = scope.generateUidIdentifier("m");
if (isAssignment) {
scope.push({ id: cloneNode(tempId) });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ var _x = {
};

(() => {
var _m, _p, _p2, _m2;
var _p, _p2, _m;

_m = [{
y: 1
}, C], [{
[{
y
}, _p, ..._p2] = _m, _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), x = _m2 === void 0 ? y : _m2, z = _p2;
}, _p, ..._p2] = [{
y: 1
}, C], _m = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), x = _m === void 0 ? y : _m, z = _p2;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ var _x = {
};

(() => {
var _m, _p, _m2, _m3;
var _p, _m, _m2;

var x, z;
_m = [C], [..._p] = _m, _m2 = _p[0], _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), x = _m3 === void 0 ? 1 : _m3, z = babelHelpers.objectWithoutProperties(_p, _excluded);
[..._p] = [C], _m = _p[0], _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), x = _m2 === void 0 ? 1 : _m2, z = babelHelpers.objectWithoutProperties(_p, _excluded);
result = {
x,
z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var _x = {
};

(() => {
var _m, _p, _p2, _m2;
var _p, _p2, _m;

_m = [C], [_p, ..._p2] = _m, _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), x = _m2 === void 0 ? 1 : _m2, z = _p2;
[_p, ..._p2] = [C], _m = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), x = _m === void 0 ? 1 : _m, z = _p2;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var _z = {
};

(() => {
var _m, _p, _p2;
var _p, _p2;

let z;
_m = [0, C], [babelHelpers.classStaticPrivateFieldDestructureSet(C, C, _x).value, _p, ..._p2] = _m, x = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), z = _p2;
[babelHelpers.classStaticPrivateFieldDestructureSet(C, C, _x).value, _p, ..._p2] = [0, C], x = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), z = _p2;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var _z = {
babelHelpers.defineProperty(C, "self", C);

(() => {
var _m, _p, _p2, _p3, _m2, _m3, _p4, _m4, _m5;
var _p, _p2, _p3, _m, _p4, _m2, _m3;

let x, y, z;
_m = [C, C], [_p, _p2,, _p3] = _m, _m2 = _p === void 0 ? C.self : _p, x = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), [, _p4] = _m3, _m4 = _p4 === void 0 ? C.self : _p4, _m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m4, C, _z), y = _m5 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m5, z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3;
[_p, _p2,, _p3] = [C, C], _m = _p === void 0 ? C.self : _p, x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), [, _p4] = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), _m2 = _p4 === void 0 ? C.self : _p4, _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _z), y = _m3 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m3, z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ let x, y, z;
class C {
static #x;
static {
var _m, _p, _p2, _m2;
var _p, _p2, _m;

_m = [{
y: 1
}, C], [{
[{
y
}, _p, ..._p2] = _m, _m2 = _p.#x, x = _m2 === void 0 ? y : _m2, z = _p2;
}, _p, ..._p2] = [{
y: 1
}, C], _m = _p.#x, x = _m === void 0 ? y : _m, z = _p2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ let result;
class C {
static #x;
static {
var _m, _p, _m2, _m3;
var _p, _m, _m2;

var x, z;
_m = [C], [..._p] = _m, _m2 = _p[0], _m3 = _m2.#x, x = _m3 === void 0 ? 1 : _m3, z = babelHelpers.objectWithoutProperties(_p, _excluded);
[..._p] = [C], _m = _p[0], _m2 = _m.#x, x = _m2 === void 0 ? 1 : _m2, z = babelHelpers.objectWithoutProperties(_p, _excluded);
result = {
x,
z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ let x, z;
class C {
static #x;
static {
var _m, _p, _p2, _m2;
var _p, _p2, _m;

_m = [C], [_p, ..._p2] = _m, _m2 = _p.#x, x = _m2 === void 0 ? 1 : _m2, z = _p2;
[_p, ..._p2] = [C], _m = _p.#x, x = _m === void 0 ? 1 : _m, z = _p2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class C {
static #y;
static #z;
static {
var _m, _p, _p2;
var _p, _p2;

let z;
_m = [0, C], [C.#x, _p, ..._p2] = _m, x = _p.#x, z = _p2;
[C.#x, _p, ..._p2] = [0, C], x = _p.#x, z = _p2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class C {
}

static {
var _m, _p, _p2, _p3, _m2, _m3, _p4, _m4, _m5;
var _p, _p2, _p3, _m, _p4, _m2, _m3;

let x, y, z;
_m = [this, this], [_p, _p2,, _p3] = _m, _m2 = _p === void 0 ? C.self : _p, x = _m2.#x, _m3 = _p2.#y, [, _p4] = _m3, _m4 = _p4 === void 0 ? C.self : _p4, _m5 = _m4.#z, y = _m5 === void 0 ? C.#self() : _m5, z = _p3 === void 0 ? y.#y : _p3;
[_p, _p2,, _p3] = [this, this], _m = _p === void 0 ? C.self : _p, x = _m.#x, [, _p4] = _p2.#y, _m2 = _p4 === void 0 ? C.self : _p4, _m3 = _m2.#z, y = _m3 === void 0 ? C.#self() : _m3, z = _p3 === void 0 ? y.#y : _p3;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ var _x = {
};

(() => {
var _m = [C],
[..._p] = _m,
_m2 = _p[0],
_m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x),
x = _m3 === void 0 ? 1 : _m3,
var [..._p] = [C],
_m = _p[0],
_m2 = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x),
x = _m2 === void 0 ? 1 : _m2,
z = babelHelpers.objectWithoutProperties(_p, _excluded);

result = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var _x = {
};

(() => {
var _m = [C],
[_p, ..._p2] = _m,
_m2 = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x),
x = _m2 === void 0 ? 1 : _m2,
var [_p, ..._p2] = [C],
_m = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x),
x = _m === void 0 ? 1 : _m,
z = _p2;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ var _z = {
babelHelpers.defineProperty(C, "self", C);

(() => {
var _m = [C, C],
[_p, _p2,, _p3] = _m,
_m2 = _p === void 0 ? C.self : _p,
x = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x),
_m3 = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y),
[, _p4] = _m3,
_m4 = _p4 === void 0 ? C.self : _p4,
_m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m4, C, _z),
y = _m5 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m5,
var [_p, _p2,, _p3] = [C, C],
_m = _p === void 0 ? C.self : _p,
x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x),
[, _p4] = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y),
_m2 = _p4 === void 0 ? C.self : _p4,
_m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _z),
y = _m3 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m3,
z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ let result;
class C {
static #x;
static {
var _m = [C],
[..._p] = _m,
_m2 = _p[0],
_m3 = _m2.#x,
x = _m3 === void 0 ? 1 : _m3,
var [..._p] = [C],
_m = _p[0],
_m2 = _m.#x,
x = _m2 === void 0 ? 1 : _m2,
z = babelHelpers.objectWithoutProperties(_p, _excluded);
result = {
x,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class C {
static #x;
static {
var _m = [C],
[_p, ..._p2] = _m,
_m2 = _p.#x,
x = _m2 === void 0 ? 1 : _m2,
var [_p, ..._p2] = [C],
_m = _p.#x,
x = _m === void 0 ? 1 : _m,
z = _p2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ class C {
}

static {
var _m = [this, this],
[_p, _p2,, _p3] = _m,
_m2 = _p === void 0 ? C.self : _p,
x = _m2.#x,
_m3 = _p2.#y,
[, _p4] = _m3,
_m4 = _p4 === void 0 ? C.self : _p4,
_m5 = _m4.#z,
y = _m5 === void 0 ? C.#self() : _m5,
var [_p, _p2,, _p3] = [this, this],
_m = _p === void 0 ? C.self : _p,
x = _m.#x,
[, _p4] = _p2.#y,
_m2 = _p4 === void 0 ? C.self : _p4,
_m3 = _m2.#z,
y = _m3 === void 0 ? C.#self() : _m3,
z = _p3 === void 0 ? y.#y : _p3;

}
Expand Down

0 comments on commit 4eb651d

Please sign in to comment.