×


Why is the speed of web assembly comparable to native code?(Read 922 times)
My understanding is that wasm is very similar to direct memory manipulation thus it is very fast. But isn't this memory manipulation on top of javascript interpreter, thus should be magnitude slower since we are not directly manipulating memory but rather treating javascript primitives as memory?

Am I getting anything wrong?



Re: Why is the speed of web assembly comparable to native code? Reply #1 on: October 02, 2017, 03:02:23 am
JavaScript is dynamically typed, asm.js tries to give hints about type information at compile time. But you can never guarantee the type, because you could assign a string to an integer variable at runtime. This can force the JavaScript engine/vm to redetermine the type and memory allocation.

Where wasm is strictly typed, and only has 4 primitives. I32, I64, F32, F64.
Among other things.



Re: Why is the speed of web assembly comparable to native code? Reply #2 on: October 02, 2017, 10:14:23 am
After a wasm module is downloaded, the browser compiles it to native code. But this generated native code follows a set of rules as to avoid vulnerabilities (Mozilla devs wrote blog posts about their implementation and probably did a video presentation in "air mozilla").

So basically, wasm is fast because it's compiled to native code, but it is a little bit behind unrestricted native code because of security measures.



Re: Why is the speed of web assembly comparable to native code? Reply #3 on: October 02, 2017, 05:26:23 pm
Because with Javascript, after downloading all the code, your browser has to first interpret it before being able to execute it. With Webassembly it can skip the interpretation part and execute it directly, saving a lot of time.



Re: Why is the speed of web assembly comparable to native code? Reply #4 on: October 03, 2017, 12:38:23 am
execute it directly
after compiling it to machine code.



Re: Why is the speed of web assembly comparable to native code? Reply #5 on: October 03, 2017, 07:50:23 am
Here's a good overview:
https://hacks.mozilla.org/2017/02/what-makes-webassembly-fast/

- Wasm is smaller and thus quicker to download than JS
- Wasm is much easier to parse / decode than JS, saving CPU time
- Wasm is easier to optimise which means it runs faster
- Wasm does not have garbage collection



Re: Why is the speed of web assembly comparable to native code? Reply #6 on: October 03, 2017, 03:02:23 pm
Javascript primitives are backed by slabs of memory, WASM operates on memory behind the primitives. There's no interaction with boxed JS objects unless you're passing ints into JS funcs you've imported
That addresses the memory front. Compute speed's a whole other topic