loading...
Cover image for Why is Web Assembly faster than Javascript ?

Why is Web Assembly faster than Javascript ?

courseprobe profile image Course Probe ・1 min read

WebAssembly is faster than JavaScript in many cases because:

  • Fetching WebAssembly takes less time because it is more compact than JavaScript, even when compressed.

  • Decoding WebAssembly takes less time than parsing JavaScript.

  • Compiling and optimizing takes less time because WebAssembly is closer to machine code than JavaScript and already has gone through optimization on the server side.

  • Reoptimizing doesn’t need to happen because WebAssembly has types and other information built in, so the JS engine doesn’t need to speculate when it optimizes the way it does with JavaScript.

  • Executing often takes less time because there are fewer compiler tricks and gotchas that the developer needs to know to write consistently performant code, plus WebAssembly’s set of instructions are more ideal for machines.

  • Garbage collection is not required since the memory is managed manually.

source: https://qr.ae/TzNksn

Discussion

pic
Editor guide
Collapse
pentacular profile image
pentacular

And in many cases it is slower, due to the interfaces required to talk to it. :)

So you will want to think carefully about the kinds of task that are appropriate to delegate to web assembly, and the kind of task that are better kept closer to the DOM and other browser resources.

Collapse
iamschulz profile image
I am Schulz

Fetching WebAssembly takes less time because it is more compact than JavaScript, even when compressed.

I'm pretty sure bundles tend to be larger, because you need to ship the entire runtime with each binary. Javascript doesn't need to do that because the engine is already in the browser.