Skip to content
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

Poor constant propagation through destructuring assignments #4219

Open
KimlikDAO-bot opened this issue Feb 10, 2025 · 2 comments
Open

Poor constant propagation through destructuring assignments #4219

KimlikDAO-bot opened this issue Feb 10, 2025 · 2 comments

Comments

@KimlikDAO-bot
Copy link
Contributor

Google closure struggles with constant propagation through destructing assignments, which, unfortunately happens very frequently in modern js code.

const { A: a, B: b } = { A: 1, B: 2 }
console.log(a, b);

google-closure-compiler --js a.js -O ADVANCED outputs

const {g:a,h:b}={g:1,h:2};console.log(a,b);

Many function call happen through "object pattern" parameters in modern js and google closure does no constant propagation through them nor does it have "re-write to normal params" kind of pass.

@trevorade
Copy link
Contributor

Thank you for your report! We'll attempt to improve this when we can.

You could experiment with setting --language_out=ECMASCRIPT5 to see if that gives you better results. It should fix this very specific issue but comes with transpilation of other newer JS features so may not work in your case.

@KimlikDAO-bot
Copy link
Contributor Author

Thank you, i didn't expect this result! We work with --language_out=UNSTABLE currently and I'm not sure if we can feasibly do es5 output.

ObjectPattern = ObjectExpression declarations or assignments are very frequent in modern js. They occur in jsx transformed js or whenever object pattern parameters are preferred for variable name clarity.

Actually the situation is a bit worse than I initially wrote:

const { A: a, B: b } = { A: 1, B: 2, C: 3, D: 4 }
console.log(a, b);

will compile to (via google-closure-compiler --js a.js -O ADVANCED)

const {g:a,h:b}={g:1,h:2,i:3,j:4};console.log(a,b);

I think there are multiple low hanging fruits here.

  1. Remove unused object expression keys: At an early pass, remove the keys of the object expression which do not match, and move the corresponding value expression out of the object expression, right before the current declaration / assignment.

  2. For ObjectPattern = ObjectExpression assignments, simply convert them to a sequence of regular assignments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants