Kotlin.js file size

As I understand, client side must always load the kotlin.js file first, then our application js files. I guess, client will never need kotlin.meta.js (123 KB) and this file is only used when transpile kotlin to js, is that correct?

The kotlin.js file size is 1.21 MB. If I minify this file, I will get ~600 KB (use jscompress.com). Compress the minified js to gz will reduce size to ~100 KB (Result of Online TAR.GZ generator). For comparison, here is the size of other frameworks: Sizes of JS frameworks, just minified + minified and gzipped, (React, Angular 2, Vue, Ember) · GitHub. Size of React is quite small when compare to Kotlin or Angular 2.

Don’t you think it is too big when using for web frontend because user needs to wait to download a big js file? Is there a plan to separate into smaller modules and only include what we need? Or does anyone have ideas to improve startup time when user visit our website the first time?

4 Likes

Sure, we are working on it. There are several possitble solutions to reduce kotlin.js size:

  1. Write our own tool that removes unnecessary parts of JavaScript files produced by Kotlin compiler.
  2. File-to-file compilation, when each Kotlin source file compiles to single JavaScript file. Tools like WebPack should remove unnecessary JavaScript files.
  3. Generate code for Google Closure Compiler. It’s very specific way, since GCC in advanced mode imposes restrictions on the code, and not all JavaScript libraries follow these restrictions.
  4. Compile program as a whole. This allows to perform dataflow analysis during compilation and derive which code is ever used.

I think we need all four options implemented someday. However, the easiest way for now is (1). We have a prototype of such tool that is capable of reducing miminal Hello Word application down to ~60 kilobytes. Another approach that we can implement in the near future is (2), it’s important, since modern JavaScript ecosystem uses file-to-file compilation.

3 Likes

Let me adress the elephant in the room, instead of a fancy transpile and glorified minification system how about grouping by purpose all functions in different js files in a class like fashion and using AMD (RequireJS for example) this will require files async allowing fast loads no matter how absurdly big kotlin.js becomes.

Kotlin should take lib size seriously, come on 1mb file just for a cleaner syntax, no one besides heavy single paged enterprise apps (and not even them) is willing to make the webapp that heavy, is simply unacceptable especially when EcmaScript 6 is about to become de facto in all browser.

I belive Kotlin has a bright future but this kinds of decisions will determine its path at least in the near future, and imo the weight of kotlin.js is a con that currently wins any posible pro and I belive there is many more people on the same boat.

3 Likes

GCE tooll available in 1.1.4 dev build reduces std lib size to ~300kb on my medium sized project(about 2mb compiled)
I would say this is god enough.

It also will be very good to have an official CDN for kotlin std lib.

2 Likes

300kb is of course bereable but this will grow over time especially the more complex your app gets this will just not happen using AMD async pattern.

it is 300kb pre minified so about angular 1.x size (minified) or 2x jquery
current js output is nowhere as good as say GWT output but i would argue after GCE tool and minification code size is not that big of a problem already.
the more complex app gets the larger part of it is app code itself.
current js compiler output looks like very direct translation of classes which is good for in browser debugging with sourcemaps or not. I yet to see other way to debug js targeted solutions without browser that actually works good. GWT had one few years ago but it was way too slow, tricky to use and very complex inside for non trivial projects.

Maybe this is the reason why Kotlin.js cannot be packaged into react native js bundle

It can’t be packaged by react-native because it’s too large. If you use the DCE tool, it can be. https://kotlinlang.org/docs/reference/javascript-dce.html

I’m trying out some better solutions, such as compiling the stdlib into multiple js files.

2 Likes

Since it’s been a while since @Alexey.Andreev posted possible solutions, I thought I’d ask if there is any progress on this issue? (I’m pretty new to the community, so I’m not sure whether “near future” means months or years!)

Has anything happened to the pruning tool prototype you spoke of, Alexey?

The prototype Alexey mentioned was released in 1.1.4 (as @Konstantin-Khvan already mentioned)

At the moment you can use it either as a standalone tool or as a Gradle plugin.

1 Like

One more data point: I work for a prominent tech. company. Our Web Tools team considered, and ruled out the use of Kotlin specifically because of this issue. It seems that until the Kotlin team gets minimal overhead in the 20K range minified/gzipped, Kotlin/JS will be ruled out everywhere but the edge cases.

1 Like

I didn’t check the last version of the dead code plugin, but it looks promising.

I’m wondering, though, since tree shaking is something that is not specific at all to kotlin, what is the state of the art nowadays in generic tree shaking tools in the nodejs community ? I don’t mean standard ones based on import and export, but advanced ones that use a symbols dependency analysis.

I’m also wondering if there is a way to generate DCE statements using code coverage test cases. The tree shaking would keep everything which is used by the tests (plus the ones declared in the gradle build file).

Since previous posts are quite old, it would be great to have an update of the situation.

Would for instance a tool like rollupjs produce interesting results compared to the DCE plugin? How would you invoke rollupjs from gradle.kts to be able to compare the results?