Size of js_of_ocaml generated files

Warning: I’m a complete beginner in js_of_ocaml, what I’m writing below is my only experience with it.

I tried the doc here: https://dune.readthedocs.io/en/latest/jsoo.html

echo 'print_endline "hello from js"' > foo.ml
echo "(executable (name foo))" > dune
dune build ./foo.bc.js

The resulting file _build/default/foo.bc.js weights 2260Kb!
Now I go to the other doc: http://ocsigen.org/js_of_ocaml/3.5.1/manual/overview
and

ocamlfind ocamlc -package js_of_ocaml -linkpkg -o foo.byte foo.ml
js_of_ocaml foo.byte

and now the resulting file foo.js is “only” 12Kb.

What happens here?

1 Like

Dune supports two modes of compilation

  • Direct compilation of a bytecode program to JavaScript. This mode allows js_of_ocaml to perform whole program deadcode elimination and whole program inlining.
  • Separate compilation, where compilation units are compiled to JavaScript separately and then linked together. This mode is useful during development as it builds more quickly.

The separate compilation mode will be selected when the build profile is dev , which is the default. There is currently no other way to control this behaviour.

Probably, this.

I’m lost, can you elaborate? (how do I select the build profile?)

Well I’ve quoted from Dune Documentation. So I guess you can look over there.

Seem like this is the link. Check the dune version though.

1 Like

By default dune builds he projects in dev mode, meaning including a series of flags to speed up compilation and have good debug information. If you want a stripped out binary, you need to build in release mode. If I am not mistaken this is just dune build --profile=release.

You can use the dune-workspace file if you want to preselect a different profile by default (not sure if this is a new dune 2 feature or was there, I never used it): https://dune.readthedocs.io/en/stable/dune-files.html

3 Likes

Thanks for the explanations, I had never used dune profiles before.

yes it works!
writing a dune-workspace also works (with dune 1.11)

Contrary to what the cited doc seems to suggest, here compiling with the “release” profile is much faster. But that’s probably because the program here is too trivial.

out of curiosity, what is the artifact size in dune release mode (compared to the 12KB of js_of_ocaml and 2260 KB in dune development mode)?

In my environment, running the example above with the default dev profile I get

-rw-r--r--  1 mseri  staff  2534414 Nov 29 16:05 _build/default/foo.bc.js

While with --profile=release I get

-rw-r--r--  1 mseri  staff  12918 Nov 29 16:05 _build/default/foo.bc.js

Which are consistent with the numbers above.

EDIT:
I have also tried it on a beefier example: the js_of_ocaml demo using owl_ode.
In that case I get the following

-rw-r--r--  1 mseri  staff  6218237 Nov 29 16:13 _build/default/teaching_material/preypredator.bc.js
-rw-r--r--  1 mseri  staff  92098 Nov 29 16:14 _build/default/teaching_material/preypredator.bc.js
2 Likes