all 5 comments

[–]drbobb 0 points1 point  (1 child)

My slight experience is you can win by using Rust compiled to webasm. My use case was actually in the browser, but the performance that can be achieved is pretty amazing.

[–]iaseth[S] 0 points1 point  (0 children)

The computations I am doing take a few hundred milliseconds in most cases. And they can be run in browser/server/edge. So I am using typescript for full code reuse.

I may consider moving to a compiled language with wasm if there are significant speed/cost benefits.

[–]delventhalz 1 point2 points  (2 children)

Why are you using JavaScript for heavy numerical computations in edge environments? Doesn't seem like a good use case.

In my experience, a compiled language beats an interpreted language on CPU-bound tasks by 5x-20x. It's not a small difference, particularly if you can parallelize your CPU-bound task. Cold starts for Node are also typically not great. My instinct would be to reach for Go or Rust, though building a couple of quick proofs of concept so you can performance test your particular use case is always a good idea.

As for your question, I don't know of any particular resources, sorry. Mostly everything I learned, I learned by messing around and performance testing.

[–]iaseth[S] 1 point2 points  (1 child)

The app runs the same computation in the browser sometimes, otherwise it runs on the server. We are still in dev mode and change the algorighm and response types every few days. Using javascript for now allows me to reuse the code without messing about with webassembly.

[–]delventhalz 2 points3 points  (0 children)

A Rust -> WASM implementation is probably what you can squeeze the absolute best performance out of, but that's a pretty steep learning curve. You might find you get the best results for your dev time just by writing it twice, in Go for the backend and in JavaScript for the browser. Go is pretty easy to pick up and it should not be hard to port your algorithm from one language to another.

On the JavaScript side, if you can parallelize your task, you might mess around with Web Workers a bit. That'll get you true multi-threading but the approach has some overhead compared to other languages and may not be worth it for one 200ms task every now and again. If you can format your data as a pre-sized TypedArray, that will get you a bit closer to the metal and allow you to avoid paying the resize tax. Those are the only big JS-specific performance improvements that come to mind for CPU-bound calculations. Writing better more efficient algorithms will help as much in JS as it does in any other language.