什么是 WebAssembly?
Author: 图恩Category: 编程开发Views: 915Words: 2046Published: 2022-06-16 **WebAssembly: A New Dawn in Web Development**
For over two decades, the only programming language capable of running natively in a Web browser has been JavaScript. The gradual decline of third-party binary plugins has excluded languages like Java and Flash from being viable candidates for web development. Other web languages, such as CoffeeScript, can only compile to JavaScript within a single day.
Today, we are entering a new era with WebAssembly (Wasm), a groundbreaking possibility. WebAssembly is a compact, high-performance binary format designed to deliver near-native performance for web applications. It aims to become a compilation target for any language, with JavaScript being just one of many.
Currently, all major browsers support WebAssembly, and it's time to seriously consider developing client applications that can be compiled into WebAssembly. Notably, WebAssembly applications do not intend to replace JavaScript applications—at least not yet. Instead, they are viewed as partners to JavaScript. JavaScript offers flexibility, dynamic typing, and human-readable source code, while WebAssembly provides speed, strong typing, and compact binary formats.
Developers should consider using WebAssembly for performance-intensive use cases such as games, music streaming, video editing, and CAD applications. Many online services, like Google Earth, have already embraced WebAssembly. Figma, a collaborative drawing and chart application, has adopted WebAssembly despite its relative novelty, to reduce load times and execution speeds.
**How WebAssembly Works**
WebAssembly, developed by the W3C, is described by its creators as a "compilation target." Developers do not directly write WebAssembly; instead, they write in their preferred language and then compile it into WebAssembly bytecode. This bytecode runs on the client side—typically in a web browser—where it is translated into native machine code and executed at high speed.
WebAssembly code is designed to load, parse, and execute faster than JavaScript. While Web browsers still incur the cost of downloading and setting up Wasm modules, these modules can range in size from a few megabytes, making such delays significant for larger projects. However, under the same conditions, WebAssembly runs more efficiently.
WebAssembly also provides a sandboxed execution model based on the same security model present in JavaScript. Wasm applications cannot access anything outside the sandbox, including the DOM of the webpage they are running on. All interactions with the machine's other parts must go through WebAssembly system interfaces (like WASI), which provide controlled access to frequently needed files, network connections, system timers, and other system services.
Currently, running WebAssembly in web browsers is the most common use case. However, WebAssembly's purpose extends beyond web-based solutions. The Wasmer project runs WebAssembly applications on server-side, similar to how Node.js runs JavaScript outside of the browser.
**Use Cases**
The most basic application of WebAssembly is as a target language for browser-based software. Components compiled into WebAssembly can be written in any of the languages available, and the final WebAssembly payload is then delivered to the client via JavaScript.
WebAssembly is designed to address performance-intensive, browser-based use cases: games, music streaming, video editing, CAD, encryption, and image recognition, among others.
In general, when determining your specific WebAssembly use case, focusing on these three domains is beneficial:
1. **High-performance code already present in the target language** – For example, if you have a high-speed mathematical function written in C and want to integrate it into a web application, you can deploy it as a WebAssembly module. Performance-critical parts of the application can remain in JavaScript.
2. **High-performance code that requires careful development** – Where JavaScript is not ideal. Previously, developers might have used asm.js for such tasks. You can still do this, but WebAssembly is positioned as a more sustainable long-term solution.
3. **Porting desktop applications to the web** – Many of the technical demonstrations of asm.js and WebAssembly fall into this category. WebAssembly can provide a foundation for more ambitious web applications than just HTML-based GUIs. For example, viewing WebDSP demonstrations in the browser and Windows 2000 demonstrations can serve as two examples.
If your existing JavaScript application has not reached any performance limits, you may not need to intervene in this development phase. However, if you need your application to run faster, WebAssembly may offer assistance.