Will Scala.js ever produce small bundles?
Asked Answered
A

1

7

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?

Abeu answered 24/1, 2018 at 3:17 Comment(2)
Define smallish? I hadn't heard of ReasonML, it looks like OCaml, i.e it has incredibly ugly syntax... like bucklescript...Nalepka
@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.Abeu
N
16

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

Nine answered 24/1, 2018 at 14:56 Comment(10)
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) github.com/scala/collection-strawman . Being involved so much in the community, what do you make of it?Abeu
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.Nine
Ah, ok, that makes sense. Thank you!Abeu
Is the discussion about the new collections library attempting and failing to have less interdependencies accessible online anywhere?Fluvial
There was no discussion. It's just an observation I made looking at the PRs that made it to the new collections over time.Nine
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.Coplanar
It is already 2022, does this project fill in the gap? github.com/scalacenter/scalajs-bundlerHaggis
scalajs-bundler has nothing to do with the question, nor the answer...Nine
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 bundlingHaggis
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.Nine

© 2022 - 2024 — McMap. All rights reserved.