# Replace jsoo/link with cat or esbuild?

**URL:** <https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600>\
**Category:** Learning\
**Created:** [March 6, 2023, 3:45pm UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600 "2023-03-06T15:45:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 6, 2023, 3:45pm UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/1 "2023-03-06T15:45:56Z")

</div>

Quoting [Separate compilation](https://ocsigen.org/js_of_ocaml/latest/manual/separate-compilation)

> js\_of\_ocaml link my-runtime.js stdlib.js libname.js modname.js std\_exit.js-o myexe.js

Is there a way to replace the `js_of_ocaml link` with `cat` or `esbuild` ? If not, what else does `js_of_ocaml link` do ?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [March 6, 2023, 4:15pm UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/2 "2023-03-06T16:15:21Z")

</div>

> [@zeroexcuses](#):
>
> If not, what else does `js_of_ocaml link` do ?

The next version of `js_of_ocaml` [will match](https://github.com/ocsigen/js_of_ocaml/pull/1378) the library archive link model of `ocaml` which is that unused compilation unit of archives are not linked in unless used by the program or `-linkall` is specified.

In that scenario I expect `js_of_ocaml link` to perform the compilation unit pruning from the library `.js` files.

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 6, 2023, 5:04pm UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/3 "2023-03-06T17:04:29Z")

</div>

I apologize for not being clear. My main focus is:

> [@zeroexcuses](#):
>
> Is there a way to replace the `js_of_ocaml link` with `cat` or `esbuild` ?

I.e. I am looking to minimize my dependency on js\_of\_ocaml. I am looking for a situation where I use it only to recompile updated \*.cmo files, then using something blazing fast (like cat or esbuild) to combine everything together.

Phrase another way: the separate-compilation out put of jsoo – is it safe to join it using cat/esbuild, or is using `jsoo link` a hard requirement for the final step ?

---

<div class="post-metadata">

**Author:** ![hhugo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hhugo/32/1143_2.png) [@hhugo](https://discuss.ocaml.org/u/hhugo)\
**Post date:** [March 6, 2023, 10:15pm UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/4 "2023-03-06T22:15:22Z")

</div>

It depend on what you mean by safe. As mentioned by @dbuenzli, with the next version of jsoo, `jsoo link` will perform some compilation unit pruning for libraries. So cat will not longer be equivalent.

`jsoo link` also does the merge of sourcemap when requested.

`jsoo link` is supposed to be fast (but maybe when sourcemap is requested). In particular, it doesn’t parse the javascript. I don’t know what you’re trying to achieve, but if `jsoo link` is too slow for you, contributing to jsoo could be a better use of your time (compare to working around the tool).

I’m curious, have you witnessed `jsoo link` being slower that esbuild ? do you have numbers ?

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 6, 2023, 10:32pm UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/5 "2023-03-06T22:32:24Z")

</div>

> [@hhugo](#):
>
> It depend on what you mean by safe.

I can run the output js. With the exception of potential slowdowns, the code outputs the same answer.

> [@hhugo](#):
>
> I’m curious, have you witnessed `jsoo link` being slower that esbuild ? do you have numbers ?

I do not have numbers. I have a fairly slow incremental rebuild with try-eio. I am currently trying to dissect all the parts and build it manually w/o dune.

I have try-eio split into “server” (merlin-js + js\_top\_worker) and “client” (codemirror + merlin\_js\_rpc / js\_top\_worker\_rpc) parts.

I can state that for the “client” part:

- rescript individual files + combine with esbuild = blazingly fast
- jsoo = very slow incremental builds

---

<div class="post-metadata">

**Author:** ![hhugo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hhugo/32/1143_2.png) [@hhugo](https://discuss.ocaml.org/u/hhugo)\
**Post date:** [March 7, 2023, 12:05am UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/6 "2023-03-07T00:05:31Z")

</div>

> I can run the output js. With the exception of potential slowdowns, the code outputs the same answer.

Then it’s not 100% safe as linking compilation unit can perform side effects. Once `jsoo link` start omitting compilation units, running the code could output different answers.

> jsoo = very slow incremental builds

How slow is very slow ? Your comments would be more actionable if it came with datapoints. Do you have a git repo to reproduce the issue ?

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 7, 2023, 12:09am UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/7 "2023-03-07T00:09:40Z")

</div>

> [@hhugo](#):
>
> Then it’s not 100% safe as linking compilation unit can perform side effects. Once `jsoo link` start omitting compilation units, running the code could output different answers.

Thank you for clarifying this.

> [@hhugo](#):
>
> How slow is very slow ? Your comments would be more actionable if it came with datapoints. Do you have a git repo to reproduce the issue ?

I do not have a git repo yet. I apologize for this; I can see how my posts are quite frustrating from your perspective. (I was actually expecting a response of: it’s equiv to running esbuild w/ the following flags, and was not prepared for this line of questioning.)

---

<div class="post-metadata">

**Author:** ![hhugo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hhugo/32/1143_2.png) [@hhugo](https://discuss.ocaml.org/u/hhugo)\
**Post date:** [March 7, 2023, 1:25am UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/8 "2023-03-07T01:25:06Z")

</div>

If you’re really after milliseconds, I would suggest to use the development version of js\_of\_ocaml-compiler as there were many changes done recently in that area (some triggered by your posts). Also, as mentioned in other posts, you should probably collect data before trying to optimize things. I suspect `jsoo link` is the fastest step of all ,most of the time. The jsoo compiler flag `--debug times` can be very useful when chasing speed improvement.

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 8, 2023, 2:38am UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/10 "2023-03-08T02:38:24Z")

</div>

Okay, I’m definitely wrong. `jsoo link` is near instantaneous.

---

<div class="post-metadata">

**Author:** ![Khady](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/khady/32/469_2.png) [@Khady](https://discuss.ocaml.org/u/Khady)\
**Post date:** [March 8, 2023, 3:04am UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/11 "2023-03-08T03:04:33Z")

</div>

Have you tried to run your dune build with `--trace-file` to see where the time is spent and what is actually slow?

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 8, 2023, 3:59am UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/12 "2023-03-08T03:59:56Z")

</div>

Thanks, was not aware --trace-file existed. What tool do you recommend for viewing the output ?

---

<div class="post-metadata">

**Author:** ![hhugo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hhugo/32/1143_2.png) [@hhugo](https://discuss.ocaml.org/u/hhugo)\
**Post date:** [March 8, 2023, 4:53am UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/13 "2023-03-08T04:53:29Z")

</div>

One remark: I don’t think you should delete your posts, you’re just hidden context and it makes it harder to follow the discussion. One just see `Okay, I’m definitely wrong` but one no longer see what you’re referring to

Trying to compile js\_of\_ocaml itself to JS. The compiler is about 30k loc.

1. senario: edit a file in compiler/bin\_js-of\_ocaml/
  - running ocamldep : **10ms**
  - recompiling the ml file to cmo : **35ms**
  - recompiling the cmo to javascript : **41ms**
  - comping jsoo link : **125ms**

2. senario: edit a file in compiler/lib  
In addition to the previous steps we now have theses two extra steps.
  - recompiling cma: **21ms**
  - recompiling cma to js: **1700ms**

Recompiling the cma.js file is quite slow, but it’s a big library. We could change the granularity of incrementality in dune and work at the level of cmo for libraries in the work tree.

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 8, 2023, 5:24am UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/14 "2023-03-08T05:24:02Z")

</div>

> [@hhugo](#):
>
> One remark: I don’t think you should delete your posts, you’re just hidden context and it makes it harder to follow the discussion. One just see `Okay, I’m definitely wrong` but one no longer see what you’re referring to

I may be wrong – I don’t think I have deleted any posts that anyone has replied to. I have deleted posts where (1) I post a question, (2) I figure out the answer before anyone replies, and (3) I delete the post.

For this particular thread, the post I deleted to talks about unquantitative experiments with Rust, tsc, rescript, esbuild, jsoo. The post “Okay, I’m definitely wrong” refers to my earlier implied claim of `jsoo link` being the bottleneck.

---

<div class="post-metadata">

**Author:** ![Khady](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/khady/32/469_2.png) [@Khady](https://discuss.ocaml.org/u/Khady)\
**Post date:** [March 8, 2023, 5:48am UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/15 "2023-03-08T05:48:47Z")

</div>

`about:tracing` in chrome or [https://magic-trace.org/](https://magic-trace.org/) or [https://ui.perfetto.dev/](https://ui.perfetto.dev/)

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 8, 2023, 7:40am UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/16 "2023-03-08T07:40:17Z")

</div>

> [@hhugo](#):
>
> Trying to compile js\_of\_ocaml itself to JS. The compiler is about 30k loc.
> 
> 1. senario: edit a file in compiler/bin\_js-of\_ocaml/
> 
> - running ocamldep : **10ms**
> - recompiling the ml file to cmo : **35ms**
> - recompiling the cmo to javascript : **41ms**
> - comping jsoo link : **125ms**
> 
> 1. senario: edit a file in compiler/lib  
> In addition to the previous steps we now have theses two extra steps.
> 
> - recompiling cma: **21ms**
> - recompiling cma to js: **1700ms**

Would you mind posting the make/model of your CPU ? I’m genuinely wondering if your hardware is 2-4x faster than mine. I’m using Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz

---

<div class="post-metadata">

**Author:** ![zbaylin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zbaylin/32/4047_2.png) [@zbaylin](https://discuss.ocaml.org/u/zbaylin)\
**Post date:** [March 8, 2023, 7:37pm UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/17 "2023-03-08T19:37:35Z")

</div>

Sorry to slightly derail this thread, but is there some documentation about the various `--debug` flags for JSOO? The [manual doesn’t seem to mention them](https://ocsigen.org/js_of_ocaml/latest/manual/options), and running `js_of_ocaml --help` shows that `--debug` is an option but doesn’t list the arguments it can take.

---

<div class="post-metadata">

**Author:** ![hhugo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hhugo/32/1143_2.png) [@hhugo](https://discuss.ocaml.org/u/hhugo)\
**Post date:** [March 8, 2023, 9:55pm UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/18 "2023-03-08T21:55:57Z")

</div>

> Would you mind posting the make/model of your CPU ? I’m genuinely wondering if your hardware is 2-4x faster than mine. I’m using Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz

11th Gen Intel(R) Core™ i7-1185G7 @ 3.00GHz

I run my test with the dev version of js\_of\_ocaml and sourcemap disabled.

---

<div class="post-metadata">

**Author:** ![hhugo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hhugo/32/1143_2.png) [@hhugo](https://discuss.ocaml.org/u/hhugo)\
**Post date:** [March 8, 2023, 9:59pm UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/19 "2023-03-08T21:59:29Z")

</div>

> [@zbaylin](#):
>
> Sorry to slightly derail this thread, but is there some documentation about the various `--debug` flags for JSOO? The [manual doesn’t seem to mention them](https://ocsigen.org/js_of_ocaml/latest/manual/options), and running `js_of_ocaml --help` shows that `--debug` is an option but doesn’t list the arguments it can take.

It’s not documented but you can get a list of possible values by passing an invalid one.

```auto
$ js_of_ocaml --debug random fake
js_of_ocaml: option '--debug': invalid element in list ('random'): invalid
             value 'random', expected one of 'link', 'mark-runtime-gen',
             'linker', 'main', 'lifting', 'shortvar', 'output', 'gen_tc',
             'gen', 'effects', 'deadcode', 'sourcemap', 'parser',
             'global-flow', 'flow', 'times' or 'invariant'
Usage: js_of_ocaml [COMMAND] …
Try 'js_of_ocaml --help' for more information.
```

---

<div class="post-metadata">

**Author:** ![hhugo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hhugo/32/1143_2.png) [@hhugo](https://discuss.ocaml.org/u/hhugo)\
**Post date:** [March 14, 2023, 12:12pm UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/20 "2023-03-14T12:12:15Z")

</div>

> [@zeroexcuses](#):
>
> Would you mind posting the make/model of your CPU ? I’m genuinely wondering if your hardware is 2-4x faster than mine.

Would you be able to share the numbers you’re seing on your side ?

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 15, 2023, 5:56am UTC](https://discuss.ocaml.org/t/replace-jsoo-link-with-cat-or-esbuild/11600/21 "2023-03-15T05:56:18Z")

</div>

> [@hhugo](#):
>
> Would you be able to share the numbers you’re seing on your side ?

I never replicated your experiment. I added up your numbers from step 1 ~ 200ms; and that is insanely fast compared to my best config for `time dune build` ~ 600ms on a much smaller, from scratch project. [Sorry, not very scientific.]
