So the processing of the entire JavaScript code is quite huge and it takes some time. Whereas VASM, as it says, is ahead of time compilation. So at the time of compilation itself, the compiler can predict. What data types to expect, and it does not have to keep on re-optimizing your code, that saves quite a noticeable chunk of time. JavaScript variables are dynamic. For example, if you have done let a is equal to five and later you can say a is equal to a string, right? You can change the type, the data type in JavaScript and the just in time compiler has to constantly keep a track of this. But in VASM, since it is strongly typed. You cannot have this constant updation of data types and hence is a lot of data, a lot of time in the CPU optimization can be achieved with this.
And last but not the least, JavaScript objects live on garbage collection heap. So like I mentioned, since JavaScript is a high level language, we do not have the control when to allocate or deallocate the memory, the garbage collector of the JavaScript does that for us. Whereas in VASM it uses linear memory, let's see that in a short diagram. So as you can see in the JavaScript execution pipeline, the browser first parses the code, it tries to compile just in time, it tries to optimize it, then it tries to reoptimize if it understands that the variable types are changing, it tries to execute the code and at the later stage it tries to garbage collect to free up the memory.
In WebAssembly, as you can see, the pipeline is a lot simpler as a result, a lot faster as well. It just have to decode and since at the compilation time it knows and it's predictable, it does not have to constantly compile, constantly optimize and reoptimize the WebAssembly. So, well, if we are seeing such a huge gain, should I rewrite all my existing JavaScript applications in Rust? Well, I don't think so. There has been a recent trend that a lot of people are migrating their existing apps, JavaScript apps into Rust, but that is not the best approach. There are a few caveats, of course. So the first and the biggest caveat, the first biggest hurdle of VASM adoption is that you cannot directly access DOM from VASM.
Comments