Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

93k for compiled hello world #480

Open
filipesilva opened this issue May 12, 2019 · 9 comments
Open

93k for compiled hello world #480

filipesilva opened this issue May 12, 2019 · 9 comments

Comments

@filipesilva
Copy link
Contributor

Heya, I was using Lumo 1.10.1 and was trying out the compilation as described in https://anmonteiro.com/2017/02/compiling-clojurescript-projects-without-the-jvm/.

For these files:

; src/hello_world/core.cljs
(ns hello-world.core)

(.log js/console "Hello World")
; build.cljs
(require '[lumo.build.api :as b])

(b/build "src"
         {:main 'hello-world.core
          :output-to "main.js"
          :optimizations :advanced
          :target :nodejs})

Running lumo -c src build.cljs creates a 93k main.js file. I don't know if this is expected. I only use ClojureScript with Lumo, so maybe this is normal and I just don't know.

But https://stuartsierra.com/2012/06/16/why-im-using-clojurescript mentions

“Hello World” in ClojureScript, optimized and gzipped, is 18K.

and https://dev.clojure.org/jira/browse/CLJS-2696 mentions

Clojurescript 1.9.946 with advanced optimizations generates a js file with 4640 bytes.
Clojurescript 1.10.217 with advanced optimizations generates a js file with 92409 bytes.

So that kinda makes me think that the 93k size is abnormal.

Then I tried to change :target :nodejs to :target :browser and that gives me a 6kb main.js instead. But then if I change (.log js/console "Hello World") to (println "Hello world") I get a 93k bundle again.

@arichiardi
Copy link
Collaborator

Probably the println function involves some very-had-to-tree-shake core functionality. This is a well-known ClojureScript problem and the usual answer is that in case you will need to include core at some point and therefore you'll end up with more than 90kb, which is comparable to including jQuery.

This is the price we, as ClojureScript devs, are willing to pay for the the language we like but I am sure not everyone would agree 😃

@arichiardi
Copy link
Collaborator

There was also a tweet recently talking about that: https://twitter.com/p_cstl/status/1139790337742782466?s=20

@tiye
Copy link
Contributor

tiye commented Jun 16, 2019

90kb is still to bigger for many JavaScript developers if they think the size does not provide any actual features. We sometimes use Webpack to analyze the size in the project to see if we can reduce it. 90kb is definitely worthing looking into.

I'm fine for 90kb in my ClojureScript projects. But to be honest I have tens of small projects built with ClojureScript, and it might be like 90k * 20+ and in total 1800kb+ for me. Turned out Google Closure Compiler is not that powerful in some way. I would really like to try if there's some solution for reducing the number.

@arichiardi
Copy link
Collaborator

arichiardi commented Jun 16, 2019

JavaScript developers if they think the size does not provide any actual features.

I think this would be the main point. For me a "feature" of ClojureScript is maintenability so I would also always make that trade-off. I understand though not everybody is on the FP+simplicity boat in which case they would not probably use ClojureScript to begin with.

@tiye
Copy link
Contributor

tiye commented Jun 17, 2019

It's trade-off. Meanwhile ReasonML is another example of functional programming language that we can compile to JavaScript with simplicity and maintainability with less time and get smaller size. Although the language is different and Clojure has too many features, I think the main factor is ClojureScript does not has its own code optimizer and Google Closure Compiler does not try to fit into the case of ClojureScript. One day when ClojureScript is compiled to WASM and there a powerful optimizer for WASM, ClojureScript would get a smaller size.

@arichiardi
Copy link
Collaborator

Meanwhile ReasonML is another example of functional programming

Agree it is tradeoffs, but there another interesting thing to point out - does ReasonML include persistent data structures? If so and it is smaller, then I would definitely like to know what they are doing and port it over to ClojureScript. See, for me maintainability comes directly from immutability as default (yes you can rarely escape but it is discouraged, like in Clojure). More specifically this is the trade-off worth making IMHO.

@arichiardi
Copy link
Collaborator

arichiardi commented Jun 17, 2019

Also worth noting that things like https://github.com/santoshrajan/lispyscript exist folks that like lisp but don't need immutability as first class citizen.

@filipesilva
Copy link
Contributor Author

Personally I don't have a strong opinion one way or the other regarding the design and trade-offs, but rather wondered if there was some overhead related to Lumo's implementation. Like injected code that introduced references that couldn't be shaken out.

@tiye
Copy link
Contributor

tiye commented Jun 17, 2019

I don't know in detail and OCaml turns out to have mutable Vectors somehow, this is a different from Clojure. But I do heard that they are doing lots of analysing and optimizing during compile time to generate smaller and faster code: https://github.com/BuckleScript/bucklescript/wiki/Why-bucklescript-matters-for-Javascript-platform#how-does-it-compare-with-other-transpilers

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants