-
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
Suggestion: put prototype in var for better minification #9638
Comments
Updating title to "minification" since that's usually what people call it when optimizing for download size |
This sounds like it could be a suggestion for the Closure compiler. It does these sorts of rewrites in other circumstances, so perhaps there's a good reason it doesn't do it here. |
@evmar I thought of that, but since Closure Compiler is not the only minification tool I figured it might be better to change the TS output itself. I can switch minification tools every day, but converting my code to a different language is a lot less likely to happen. |
I'm not sure whether this would really reduce the gzipped output size. After gzipping, the string 'prototype' is only stored once. It would be interesting to see some numbers on this, maybe someone can create, well, a prototype 😅 and compare the numbers. |
Ungzipped size still matters for parse speed, but that's of course much harder to measure deterministically |
Minifiers (Closure/Uglify/etc.) will typically not mangle property names, because it doesn't know which are "safe". Then there are property names that are "magical" like My personal opinion is that down-emitting in the fashion suggested above would not have any negative consequences and doesn't make the down emit unclear (except maybe instead of the terse |
The output for subclasses could be shrunk further if |
Accepting PRs. Be sure to handle name collisions (e.g. a reference to an outer |
This bug was filed in 2016, which is possibly before Closure Compiler supported ES6 classes natively. Today, I think the right way to solve this problem is to have TS and Closure emit ES6, or if you must downlevel you could downlevel with Closure. @GeneGenie you can change the target emit to ES6 in the playground to fix your output too. |
Yeah, I agree this isn't worth any additional effort at this point. |
I recently developed a module in TS, meant for the web, so it is also being minified.
I reviewed the output file and found that the word
prototype
is not minified (naturally, since it is a reserved word). But my module ended up having 153 occurrences ofprototype
, which amounted to 1.34 KB out of a total of 21KB in the minified file (I am using Google Closure Compiler with the ADVANCED_OPTIMIZATIONS flag).So I had an idea that can reduce that by putting the
prototype
into a variable before using it in every class definition.Example:
Code
Actual behavior:
Suggested behavior:
The text was updated successfully, but these errors were encountered: