Everything You Need to Know About WebAssembly
- How does WebAssembly enable these languages to run natively inside the browser?
These are three critical questions that any curious developer new to WebAssembly may ask. Let's address each of these questions one by one:
1) How WebAssembly Works
When you write a C# Console application for Windows, you typically run the
dotnet build command or use the "Build" button in Visual Studio. The C# compiler translates your code into an intermediate representation called "IL" (Intermediate Language). This IL format is not yet the final binary that the machine understands. However, when you open the executable output file, the .NET runtime starts interpreting these instructions and executes them.
More recently, .NET has introduced Ahead of Time (AOT) compilation, which compiles your code directly into the final format based on the platform and CPU architecture that will execute your code. This results in faster execution, as the code is available in binary format, unlike when the runtime must translate and execute IL instructions on the fly.
WebAssembly operates in a similar fashion. Browser vendors, including Microsoft, Firefox, Apple, and Google, have developed WebAssembly as a new binary format and compilation target. The output of your code, compiled to WebAssembly, can run natively in most modern browsers, much like C++ applications run on Windows or Mac machines.
For example, when you write C++ code, you can use a tool called Emscripten. This tool acts as a full C/C++ compiler for WebAssembly. You can write standard C++ code or use existing code and compile it into a WebAssembly module with the ".wasm" format.
The compilation process resembles the earlier .NET scenario. When you compile your C++ code to WebAssembly, it generates an Intermediate Representation (IR), resulting in a ".wasm" file. When the browser downloads this file, it compiles it directly while fetching the WebAssembly module to the client, tailored to the computer architecture on which the browser is running, such as "x86" or "ARM."
This way, when the WASM module is downloaded to the browser and initialized, the code can be executed based on the module's instructions.
2) Is WebAssembly Secure?
Because of the browser app contstraints memory access limtations, and the security check like accessing the file system, the WASM module has to communite with JS for those concerns which downgrades the pure performance of compiled language just a bit and that's why you hear the sentence WebAssembly runs in a near-native performance
WebAssembly's performance advantages come from its binary format and close-to-the-metal execution capabilities. It provides a predictable and optimized runtime environment, making it suitable for computationally intensive tasks, such as games, simulations, and multimedia applications.
However, it's important to note that the actual performance gains can vary depending on the specific use case and the efficiency of the code. Careful optimization is still required to achieve the best results.
Microsoft introduced Blazor in 2019 as the first official framework to build modern web applications using C# that run fully native in the browsers. So, if you want to kick off your journey with WebAssembly, Blazor is the key.
Thanks for reading