WebAssembly: The JavaScript Killer?
The hype around WebAssembly could easily lead you to believe that it has the potential to replace JavaScript. Here, I will explain both WebAssembly and JavaScript, and why I think their future looks more like coexistence than competition. We will specifically be talking about browser-based WebAssembly.
What is JavaScript?
JavaScript is a platform agnostic programming language, meaning that any device that runs a browser will run JavaScript. It is commonly used as the basis for mobile and desktop applications, server-side rendering and deployment, and WebUI.
Although it forms the backbone of many applications that we rely upon, JavaScript also draws major complaints. It can tend to be slow, or cause browsers to flicker or reduce their loading speed. It is also not equipped for highly sophisticated numbers and number operations, like in the case of computer-aided design calculations. For example, large numbers can lose their accuracy when operations are performed on them in JavaScript.
An important process to understand for this comparison between JavaScript and WebAssembly is how they “compile” code. Compiling is the process of transforming human-readable code, written in a scripting language, into machine code, which is the language that the computer understands. JavaScript compiles while it runs in the browser, which can be a cause of slow performance.
What is WebAssembly?
WebAssembly (also known as Wasm) is a programming language made up of a universal low-level bytecode. This low-level bytecode is a closer format to machine code than the programming language of JavaScript. In contrast to the way that JavaScript compiles as it runs, WebAssembly finishes compiling and loading resources before running in the browser.
WebAssembly was the fourth programming language to become standard within all major browsers in December 2019 (joining HTML, CSS, and JavaScript). This acceptance as a standard recognizes a potential to contribute to the evolution of web technologies as we know them.
Many programs that you may use are already running on WebAssembly, such as:
- Adobe Acrobat’s in-browser application, which uses C++ code that has been compiled directly into WebAssembly.
- Google Earth, which uses WebAssembly to address stuttering and rendering issues that would otherwise happen with JavaScript. It’s important to note that JavaScript still ties WebAssembly into the browser (more on this later).
Why Do People Like WebAssembly?
WebAssembly remains open source, which is a highlight for those who have followed its history and value this innovation-spurring open source format. It has also been lauded as having the potential to be a, “build once, run anywhere” star that performs across all hardware and platforms. The truth of this claim is yet to be determined due to WebAssembly’s relative youth compared to its peers.
Near-Native Performance
Perhaps the most touted aspect of WebAssembly is that it can provide near-native performance, meaning that it can act more like hardware than other languages like JavaScript are capable of. In reality, it is probably only performing about 50% native CPU operations, but it is still much closer to native performance than JavaScript.
Security
When running, WebAssembly can’t reach out to the machine it is running on. This is called operating in a “secure sandbox,” and it is done by most web technologies. It generally protects users from malicious programs that could take control of their personal computers, as long as they exercise discretion by not being lured into clicking on flashy advertisements that disguise malware.
Scripting Language Versatility
Virtually any major programming language will compile to WebAssembly (C, C++, Rust, Go, TypeScript, .Net, Kotlin, and many others). This makes using WebAssembly a seamless transition for a majority of developers from various source code backgrounds.
Best Uses For WebAssembly
WebAssembly is best suited for:
- Scientific operations: The low-level type language that WebAssembly uses is not prone to the same computing issue that JavaScript has with large numbers.
- Computer-aided design: It is theoretically possible to bring programs like AutoCAD or Adobe Photoshop into WebAssembly to accomplish this.
- Complex games: As an example of what is possible, one person devoted a seven-week effort towards successfully compiling the video game Doom 3 into WebAssembly (https://www.infoq.com/news/2019/07/doom3-web-assembly-port/).
Head To Head Comparison
In a benchmark speed comparison (https://javascrpit.plainenglish.io/webassembly-vs-javascript-can-wasm-beat-javascript-in-benchmark-cd7c30faaf7a#), WebAssembly and JavaScript each differentiated themselves as winners in separate tasks. When the code is simple, JavaScript is faster when compared to WebAssembly because of its ability to run almost immediately rather than compiling before running. However, when the code is complex, WebAssembly overcomes its load time delay and performs faster than JavaScript.
Overall, WebAssembly allows for complex and sophisticated operations, while JavaScript provides simple user interactions and application structure. They are even interdependent in some respects, such as the way that JavaScript acts as the glue that connects WebAssembly modules into web browsers. Each has individual strengths that make them specialists in their own niches, and suggests a peaceful coexistence.
There you have it – WebAssembly simply can’t kill JavaScript!
Resources
The following resources were used to compile this comparison between JavaScript and WebAssembly, and would be excellent threads to continue reading if you would like to gain a deeper understanding on this subject:
https://snipcart.com/blog/webassembly-vs-javascript
https://thenewstack.io/what-is-wenassembly-and-why-do-you-need-it/
https://www.w3.org/2019/12/pressrelease-wasm-rec.html.en
https://medium.com/adobetech/acrobat-on-the-web-powered-by-webassembly-782385e4979e