I was able to archive 10x-60x speedups compared to the performance of most popular JS-only implementations.
You can make your own measurements here: https://csb-9b6mf.daninet.now.sh/
I was in particular benchmarking ECC, which is much harder to optimize in JS (and in general).
Code is here:
JS: https://github.com/bcoin-org/bcrypto/tree/master/lib/js
C: https://github.com/bcoin-org/libtorsion
To benchmark:
$ git clone https://github.com/bcoin-org/bcrypto
$ cd bcrypto
$ npm install
$ node bench/ec.js -f 'secp256k1 verify' -B js
$ git clone https://github.com/bcoin-org/libtorsion
$ cd libtorsion
$ cmake . && make
$ ./torsion_bench
$ make -f Makefile.wasi SDK=/path/to/wasi-sdk
$ ./scripts/run-wasi.sh torsion_bench.wasm ecdsaHe wants WASM to be closer to C performance. I.e. to move further along this line:
|JS|========>|WASM|=========>=========>========>|C|> Everyone says this, but I would dispute it as misleading in a lot of cases. I've been experimenting a lot with wasm lately. Yes, it is faster than javascript, but not by all that much.
I think even if it is faster in general, you might lose all that advantage as soon as you have to cross the WASM <-> JS boundary and have to create new object instances (and associated garbage) that you never would have needed to create if you had used only one language.
Therefore moving to WASM for performance reasons on a project which crosses the language boundaries very often due to browser API access doesn't seem too promising to me.
It's not a matter of speed here. It's a matter of enabling certain types of programs which are borderline impossible with pure JS runtimes.
So I will probably move most of the logic to Rust
On the WASM side: Does WASM support real threads yet? Otherwise moving to Rust wouldn't really help you? If it's just "WebWorker" like multiple runtimes, you might still pay serialization costs to move objects between workers.
Erlang's runtime does "fine" with its preemptive concurrency model, JS runtimes are a joke in this regard.
2-3x may not be the 15-16x you see in native code, but it's still a massive speedup in already optimized code, and is likely enough to make a bunch of applications that weren't quite feasible to do in on the web now feasible.
Plus things like competitive games and what I'll call "pretty" games have to squeeze out as much performance as possible, and no hitching is acceptable to competitive games, which IMO means WASM is still a no-go for those types of games(although games that don't have this requirement undoubtedly benefit)
You can do this in JS too with TypedArray and whatnot but the key word here is 'natural.'
I've been working on a game engine as a side project with C++ and WASM -- and there are already many improvements over what I was getting with JS due to less GC, better data layout (the data layout thing is also esp. helpful for managing buffers that you drop into the GPU). I don't think it was about 'pure compute' as much as these things. C++ and Rust give you tools to manage deterministic resource utilization automatically which really helps.
A bonus is that the game runs with the same code on native desktop and mobile.
Otherwise, eventually I expect WebAssembly to match what Flash Crossfire and PNaCL were capable of 10 years ago.
Does firefox have a headless mode, a standalone implementation, or some CLI tool I can use to run a WASM binary? Running stuff in the browser is cumbersome.
Why is this the case? Can we expect it to go away as browsers mature?
One thing that's often overlooked is that even though "idiomatic Javascript" using lots of objects and properties is fairly slow, it can be made fast (within 2x of native code compiled from 'generic' C code) by using the same tricks as asm.js (basically, use numbers and typed arrays for everything). But the resulting Javascript code will be much less readable and maintainable than cross-platform C code that's compiled to WASM.
That is an insane difference even if nowhere close to native code.
Some engineering fields would be crazy with a 20% gain... 200% is huge!
They even say asm.js is supposed to be "near-native code execution speeds". In my experience, it is no where close to native speed. People should avoid this kind of deceptive marketing.
But it's way more predictable, no more crazy 95th percentiles.
Everyone says this, but I would dispute it as misleading in a lot of cases. I've been experimenting a lot with wasm lately. Yes, it is faster than javascript, but not by all that much.
It's the speed of generic 32 bit C. It leaves a lot to be desired in the way of performance. My crypto library, when compiled to web assembly, is maybe 2-3x the speed of the equivalent javascript code. Keep in mind this library is doing integer arithmetic, and fast integer arithmetic does not explicitly exist in javascript -- JS is at a _huge_ disadvantage here and is still producing comparable numbers to WASM.
This same library is maybe 15 or 16 times faster than JS when compiled natively, as it is able to utilize 128 bit arithmetic, SIMD, inline asm, and so on.
Maybe once WASM implementations are optimized more the situation will be different, but I am completely unimpressed with the speed of WASM at the moment.