WebAssembly vs asm.js in ammo.js

This article compares the WebAssembly (Wasm) and asm.js builds of ammo.js, the Emscripten-ported version of the Bullet physics engine. We will break down the key differences between these two builds in terms of performance, file size, loading times, memory management, and browser compatibility to help you choose the right version for your web-based 3D applications.

Performance

WebAssembly is a binary instruction format that executes at near-native speed. Because the browser can decode and compile WebAssembly bytecode directly into machine code, the WebAssembly build of ammo.js offers superior, more consistent performance. asm.js is a highly optimizable, strict subset of JavaScript. While fast, asm.js is still bound by the limitations of the JavaScript engine, resulting in lower execution speeds and higher CPU overhead during complex physics simulations compared to its WebAssembly counterpart.

File Size and Network Load

The WebAssembly build of ammo.js is compiled into a compact binary format, making the file size significantly smaller than the asm.js build. A smaller file size translates to faster download times for users, which is crucial for web games and interactive 3D experiences where initial loading times impact user retention. The asm.js build, being plain text JavaScript, requires more bandwidth to download.

Parsing and Compilation Speed

Once downloaded, the browser must prepare the code for execution. WebAssembly is designed for fast compilation; browsers can compile the binary code almost instantly, often page-by-page as it downloads. Conversely, the asm.js build is a massive JavaScript file that the browser’s engine must parse and analyze before execution. On mobile devices or lower-end hardware, parsing the large asm.js file can cause a noticeable delay or “freeze” during application startup.

Memory Management

Both builds rely on a pre-allocated block of memory (a heap represented by an ArrayBuffer) to handle the C++ pointer-based memory model of the Bullet physics engine. However, WebAssembly manages this memory more efficiently. WebAssembly supports dynamic memory growth with less overhead, whereas resizing memory in asm.js can be slower and more prone to performance hiccups.

Browser Compatibility

Browser support is the primary area where asm.js holds an advantage over WebAssembly. Because asm.js is valid JavaScript, it will run on virtually any browser, including older legacy browsers, albeit without the performance optimizations. WebAssembly is supported by all modern evergreen browsers (Chrome, Firefox, Safari, Edge) and mobile browsers, but it will not run on outdated browsers like Internet Explorer.

Summary

For modern web development, the WebAssembly build of ammo.js is the preferred choice due to its superior performance, smaller file size, and faster startup times. The asm.js build should only be used as a fallback for projects that require strict compatibility with legacy browsers that lack WebAssembly support.