-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unoptimized destructuring compilation #11779
Comments
I would rather say this adheres to spec. var a = {};
var b = a;
[a.x, a.y, a.x] = [1, (a = {}), 3]; |
@HerringtonDarkholme, I agree that in your example it's impossible to compile code without allocation of new array. But if we look at babeljs compiler we can found what it generate optimized code if it's possible // source
function possibleToOptimizeThisFunction() {
let i = 1000;
while (i--) {
let [a, b, c] = [1, 2, 3];
}
}
function impossibleToOptimizeThisFunction() {
var a = {};
var b = a;
[a.x, a.y, a.x] = [1, (a = {}), 3];
} // compiled by babel
function possibleToOptimizeThisFunction() {
var i = 1000;
while (i--) {
var a = 1;
var b = 2;
var c = 3;
}
}
function impossibleToOptimizeThisFunction() {
var i = 1000;
while (i--) {
var a = {};
var b = a;
var _ref = [1, a = {}, 3];
a.x = _ref[0];
a.y = _ref[1];
a.x = _ref[2];
}
} |
Idly curious why you're writing the intializers this way? Why not just |
I like how the output always matches the syntax being transformed, it is nicely predictable. Also, if this were added, I would want to disable it during development. I would only enable it when creating a production build because there is likely to be a performance penalty for these kinds of heuristic optimizations during transpilation. It may be small, but I would want to disable it. Unfortunately, just the notion of toggleable optimizations would introduce a lot of additional, and in my mind unnecessary complexity into the language. I real don't like the idea of having TS dev and TS prod modes as these are in the domain of other tools. You could argue that I am being hyperbolic by even hinting at this but I can imagine the issues requesting potential optimization x, or y, which breaks z that would crop up overnight. |
@RyanCavanaugh, it's not problem to use |
Accepting PRs to emit the optimized code in the case where the initializer is an array literal of sufficient length |
TypeScript Version: 2.0
Code
Expected behavior:
Actual behavior:
On each iteration tsc creates new unnecessary array _a.
The text was updated successfully, but these errors were encountered: