-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add an option to preserve program completion value #640
Comments
rollup/rollup#774 seems to have a nice summary what all the fuzz is about. |
Test cases would be nice. |
If I interpret this thread correctly it suggests putting parens around IIFEs and any inline function because the Chrome browser - and only the Chrome browser - is slower to parse otherwise? |
This issue affects Edge/IE as well. I did some benchmarks (rollup/rollup#774 (comment)) and spoke with Chakra engineers to confirm. |
Firefox and Safari do not have this problem. It's odd that two other major browsers don't optimize this common IIFE idiom. Chrome's parser is reportedly being rewritten to increase its speed which would make this less of an issue. In any case, people can set the compress option |
Based on this remark I think Microsoft would want to optimize the common
|
This specific issue is regarding completion value. Inner IIFEs would still receive the !. I can't immediately find a representative benchmark (e.g. profiles of a full application running for a few seconds) in the referenced task. If it turns out to really matter in real world applications it is worth considering removing negate_iife (or defaulting to off). On the other hand, the performance issue is more easily solved by browser vendors - as it seems it should be a trivial change. |
How does one access the "completion value" of the program through an IIFE anyway?
I can't get it to work. (Edited to mention IIFE) |
@kzc Just ran a benchmark to test (see more comments in #886 (comment)), and What's unoptimized is the UMD-style (or Browserify-style/Webpack-style/etc.) case where the function is passed in to another function. If you wrap parens around the function, though, you can get the optimization. So in short this has nothing to do with IIFEs and everything to do with parens around immediately-invoked functions (however they may get invoked). |
@kzc an example would be inlined event handlers, which can be minified: <a onclick="(function () { return false; })()"> After minification with Granted, this is easily mitigated by disabled |
@rvanvelzen Thanks for explanation. @alexlamsl does html-minifier uglify inlined JS code within HTML as per comment above? |
@kzc yes it does, by wrapping the code around So whilst the example above would produce the wrong result as stated, the more common form of |
@alexlamsl Thanks for confirming that uglify is used in the wild in that scenario. |
Using the example cited above: <a onclick="(function () { return false; })()"> It may be better if the
because although the return value is converted to a number, at least the "truthiness" of the return value remains the same:
|
Ignore my last post above. A lesser wrong is still wrong. :-) And Microsoft Edge browser reportedly optimizes the |
Yes, based on my updated benchmark (#886 (comment)), only Chakra optimizes for Also, according to the new benchmark it seems In my opinion bundlers/minifiers ought to try harder to give the engine hints, but in this case, doing a good job would be hard. You'd basically have to parse the whole JS file and try to determine which functions are immediately-invoked and which ones aren't, and then wrap the immediately-invoked ones in parens to "trick" the engines into not preparsing. Wrapping non-immediately-invoked functions in parens would be a performance regression because it would increase startup time by fully parsing functions that are only executed later (or never executed). |
Saying it's a bug is a bit harsh, as the generated code still produces the correct result in all but one pathological case above: #640 (comment) and the option can be disabled by the user should they choose. But for the few bytes
Uglify is a fairly representative javascript program and only 3 bytes are saved. Disabling |
@kzc just read #640 (comment) and thought I might be able to answer that...
|
Things like Angular Expression and Bookmarklet do not specify `return`, but implicitedly assumes the evaluated value from the final statement to be the return value. fixes mishoo#354 fixes mishoo#543 fixes mishoo#625 fixes mishoo#628 fixes mishoo#640 closes mishoo#1293
Bookmarklet for instance implicitedly assumes a "completion value" without using `return`. The `expression` option now supports such use cases. Optimisations on IIFEs also enhanced. fixes mishoo#354 fixes mishoo#543 fixes mishoo#625 fixes mishoo#628 fixes mishoo#640 closes mishoo#1293
Bookmarklet for instance implicitedly assumes a "completion value" without using `return`. The `expression` option now supports such use cases. Optimisations on IIFEs also enhanced. fixes mishoo#354 fixes mishoo#543 fixes mishoo#625 fixes mishoo#628 fixes mishoo#640 closes mishoo#1293
See #543, #625, #628.
The text was updated successfully, but these errors were encountered: