Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Scala.js ever produce small bundles?

While I love Scala the language, and Scala.js the project, I'm a little put-off by the size of the eventual JS bundle, even in fullOptJS mode.

My immediate need is to create a small-ish library for use in the browser. >150kb is a big ask, and arguably comparable tools like BuckleScript/ReasonML promise fast execution and tiny bundles.

Will Scala.js start producing smaller bundles in the foreseeable future?

like image 234
vivri Avatar asked Jan 24 '18 03:01

vivri


1 Answers

Short answer: unlikely.

When designing a language, there are always trade-offs to make. In particular, cross-compiling a language across JavaScript and another target typically leads to a series of more-or-less conflicting goals. For example, producing "idiomatic, readable JavaScript" is often at odds with producing "highly optimized JavaScript".

In that space, the main design goals of Scala.js are, in decreasing order of importance:

  1. Interoperability with JavaScript: the ability to call and be called by JavaScript code/libraries.
  2. Compatibility with Scala/JVM: unless using inherently platform-specific APIs (e.g., threads), the same Scala code should compile with Scala/JVM and Scala.js, and behave in the same way.
  3. Run-time performance: the resulting code should be as fast as possible.
  4. Code size: the resulting code should be as small as possible.

While code size is definitely a concern, as you can see, it sits pretty low on the list of main design goals. This means that code size concerns will typically yield to other concerns on the list.

In particular, it is often at odds with the requirement of compatibility with Scala/JVM. Indeed, Scala has a pretty large standard library, notably collections, and many parts of that library inter-depend on each other. This means that as soon as you use, say, a Scala List, your code needs a significant portion of the standard Scala collections library. This portion of the stdlib is what makes most non-trivial Scala.js programs weigh above 150 KB.

Since the above conditions (design goals + collections library inter-dependencies) are unlikely to change in the foreseeable future, it is equally unlikely that Scala.js will suddenly produce less code.

Strictly speaking, it is possible to write a Scala.js application producing only 10 KB or a bit more. But in order to do so, you must be very careful not to use any parts of the collections library, ever. You should use js.Arrays, js.FunctionNs and js.Promises everywhere, instead of Lists, => functions and Futures, for example. At this point, Scala.js stops being Scala, and hence you would be better off with another language (e.g., BuckleScript).

like image 188
sjrd Avatar answered Sep 23 '22 11:09

sjrd