6

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?

sjrd
  • 21,805
  • 2
  • 61
  • 91
vivri
  • 885
  • 2
  • 12
  • 23
  • 1
    Define smallish? I hadn't heard of ReasonML, it looks like OCaml, i.e it has incredibly ugly syntax... like bucklescript... – Aluan Haddad Jan 24 '18 at 03:31
  • @Aluan, it is meant to be a stateful interface to the server, at most 500 loc in vanilla js. I'm writing the server in Scala, and would like to make use of common domain & RPC. – vivri Jan 24 '18 at 03:41

1 Answers1

15

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).

sjrd
  • 21,805
  • 2
  • 61
  • 91
  • Thanks for the thorough answer, @sjrd. I've read that the base collection lib should undergo modularization in some near future, and there's a project underway to be a drop-in replacement (afaik) https://github.com/scala/collection-strawman . Being involved so much in the community, what do you make of it? – vivri Jan 24 '18 at 18:39
  • 2
    Initially, the dream was that the new collections library would have less inter-dependencies, indeed. As it grew bigger and supported more features from the old library, though, the dependencies seem to have made their way back in. It seems pretty inherent, unfortunately. – sjrd Jan 24 '18 at 20:45
  • Ah, ok, that makes sense. Thank you! – vivri Jan 24 '18 at 20:47
  • Is the discussion about the new collections library attempting and failing to have less interdependencies accessible online anywhere? – nafg Jul 10 '18 at 04:23
  • There was no discussion. It's just an observation I made looking at the PRs that made it to the new collections over time. – sjrd Jul 10 '18 at 05:46
  • http://blog.chriszacharias.com/page-weight-matters I like your work but I feel like there should be a way to keep the size of pages low. – 0fnt Sep 29 '18 at 10:33
  • It is already 2022, does this project fill in the gap? https://github.com/scalacenter/scalajs-bundler – tribbloid Mar 08 '22 at 22:01
  • 1
    scalajs-bundler has nothing to do with the question, nor the answer... – sjrd Mar 08 '22 at 23:00
  • OK I sense a lot of terminology abuse somewhere. By convention, I guess the question is actually asking for an "uglifier", while scalajs-bundler really does the bundling – tribbloid Mar 09 '22 at 17:44
  • Scala.js has had an "uglifier" since its beginning 8.5 years ago. The question goes further than a simple uglifier. It's asking for *less* actual code. – sjrd Mar 09 '22 at 20:52