GWT reduces compiled javascript size

I found that the size of compiled JavaScript is growing faster than I expected. Adding multiple lines of Java code to my project can increase the size of the script in several Kbs.

At the moment, my compiled project weighs 1 MB. I do not use any external libraries, except for those for MVP (Activities and Places), testing (JUnit) and logging.

I would like to know if there are any encoding methods / recommendations to compile the script as little as possible. I do not mean code splitting, but methods or coding patterns that can make compiled JavaScript more efficient.

Many thanks

+8
javascript gwt
source share
2 answers

GWT uses a pay-as-you-go design philosophy, and since you are not allowed to use reflection, the compiler can statically prove (by method) that a section of code is β€œavailable” and eliminate those that don’t. For example, if you never use the remove () method in ArrayList, then this code will not be included in the resulting JavaScript.

If you see a few kilobyte jumps with just a few lines added, this probably means that you have introduced the use of a new type (and possibly another that depends on other new types) that you have not yet been using. It may also mean that you have made changes to send this new type β€œby cable” back to the server, in which case the GWT generator should have enabled JavaScript to marshal this type and any new types available through its β€œhas-a” and is-a.

So, if it were me, I would start there: when you catch a 2-line change that increases the number of kilobytes, start by looking at the types and ask if this is the type you used earlier, and you send the new type by wire and whether this type depends on other types under the hood.

One final thought: in Ray Ryan's 2009 presentation on Google I / O, he mentioned superstition that he chose from the GWT compiler team, where they recommended not using generic types (I'm not talking about Java Generics here, but rather supertypes) as RPC arguments and return values. In particular, instead of having your RPC call take or return a card, take or return a HashMap instead. The belief that the GWT generator can then reduce the amount of serialization code that it must generate at compile time (because it can, for example, refrain from generating serialization code for TreeMap).

Hope this helps.

+23
source share

GWT creates different output versions for each supported browser, so when you say that the project size is 1 MB, do you mean their total size? (each browser only downloads the one that it really needs).

I tried experimenting with the generated output using various inheritance / class / generic constructs. Unfortunately, the added complexity introduced is far superior to small size improvements (when generating generics).

I worked on some large GWT projects (+50,000 lines) and found that code obfuscation combined with enabling compression on a web server is the easiest way to minimize load. If this does not reduce the code enough, check out the GWT report that you can use to identify potential problem classes and places to insert code decomposition.

+4
source share

All Articles