ArticleslgStudy

science

Asm.js

Asm.js is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Asm.js rather than just read about it. In short: asm.js is a subset of JavaScript designed to allow computer software written in languages such as C to be run as web applications while maintaining performance characteristics considerably better than standard JavaScript, which is the typical language used for such applications. asm.js consists of a strict subset of JavaScript, into which code written in statically typed languages with manual memory management (such…

Key takeaways

  • Asm.js belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Asm.js to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Asm.js from memory before moving on to harder problems.

Reference excerpt

asm.js is a subset of JavaScript designed to allow computer software written in languages such as C to be run as web applications while maintaining performance characteristics considerably better than standard JavaScript, which is the typical language used for such applications. asm.js consists of a strict subset of JavaScript, into which code written in statically typed languages with manual memory management (such as C) is translated by a source-to-source compiler such as Emscripten (based on LLVM). Performance is improved by limiting language features to those amenable to ahead-of-time optimization and other performance improvements. The project was started at Mozilla, and Firefox 22 released in 2013, was the first web browser to ship asm.js-specific optimizations. Compiler and browser makers' experience with asm.js influenced the development of WebAssembly, which has largely supplanted asm.js since its release. Firefox 148, shipped in 2026, disabled asm.js optimizations and asm.js, with old code transparently continuing to run using the main JavaScript engine.

Design asm.js enables significant performance improvements for web applications, but does not aim to improve the performance of hand-written JavaScript code, nor does it enable anything other than enhanced performance. It is intended to have performance characteristics closer to that of native code than standard JavaScript by limiting language features to those amenable to ahead-of-time optimization and other performance improvements. By using a subset of JavaScript, asm.js is largely supported by all major web browsers, unlike alternative approaches such as Google Native Client.

Code generation

asm.js is not typically written directly: instead, as an intermediate language, it is generated through the use of a compiler that takes source code in a language such as C++ and outputs asm.js. For example, given the following C code:

Emscripten would output the following JS code:

Note the addition of | 0 and the lack of type specifiers. In JavaScript, bitwise operators convert their operands to 32-bit signed integers and give integer results. This means that a bitwise OR with zero converts a value to an integer (a very simple "conceptual" presentation of bitwise operators may not deal with type conversion at all, but every programming language defines operators for its own convenience, as Javascript does here). By doing this for each parameter, this ensures that if the function is called from outside code, the value will be converted to the correct type. This is also used on the return value, in this case to ensure that the result of adding 1 to i will be an integer (as otherwise it could become too large), and to mark the return type of the function. These conversions are required by asm.js, so that an optimising compiler can produce highly efficient native code ahead-of-time. In such an optimising compiler, no conversions are performed when asm.js code calls other asm.js code, as the required type specifiers mean it is guaranteed that values will already have the correct type. Furthermore, rather than performing a floating-point addition and converting to an integer, it can simply do a native integer operation. Together, this leads to significant performance benefits. Here is another example to calculate the length of a string:

This would result in the following asm.js code:

In the generated code, the variable MEM8 is actually a byte-by-byte "view" of a typed buffer, which serves as the "heap" of the asm.js code.

Performance Since asm.js runs in a browser, the performance heavily depends on both the browser and hardware. Preliminary benchmarks of C programs compiled to asm.js are usually within a factor of 2 slower than native compilation with Clang. Much of this performance gain over normal JavaScript is due to 100% type consistency and virtually no garbage collection (memory is manually managed in a large typed array). This simpler model with no dynamic behavior, no memory allocation or deallocation, just a narrow set of well-defined integer and floating point operations enables much greater performance and potential for optimization. Mozilla's benchmark from December 2013 showed significant improvements: "Firefox with float32 optimizations can run all those benchmarks at around 1.5× slower than native, or better." Mozilla points out that the performance of natively compiled code is not a single measure but rather a range, with different native compilers (in this case Clang and GCC) delivering code of differing performance. "In fact, on some benchmarks, like Box2D, FASTA and copy, asm.js is as close or closer to Clang than Clang is to GCC. In one case, asm.js even beats Clang by a slight amount on Box2D."

Implementations The Emscripten project provides tools that can be used to compile C and C++ codebases (or any other languages that can be converted to LLVM IR) into asm.js (now WebAssembly). All browsers with support for ECMAScript 6 should be able to run asm.js code, as it is a subset of that specification. However, since features were added in that edition to enable full asm.js support (Math.fround()), older browsers lacking those features may encounter problems. Some browser implementations are especially optimised for asm.js:

Mozilla Firefox was the first web browser to implement asm.js-specific optimizations, starting with Firefox 22. OdinMonkey, Mozilla's asm.js ahead-of-time compiler used in Firefox, is a component of IonMonkey, the JIT compiler of SpiderMonkey. Microsoft was implementing support for asm.js in Chakra, the JavaScript engine used by Microsoft Edge Legacy, performing validation to produce highly optimised JIT code. The optimizations of Google Chrome's V8 JavaScript engine in Chrome 28 made asm.js benchmarks more than twice as fast as prior versions of Chrome, although Chrome's V8 does not use ahead-of-time compilation.

Adoption

Most applications using asm.js were C or C++ applications compiled using tools such as Emscripten or Mandreel. asm.js was primarily used for applications whose existing native codebases could be translated automatically for execution in web browsers. So far, a number of programming languages, application frameworks, programs, libraries, games, game engines and other software have already been ported. Some of them are given below.

Programming languages C/C++: Clang and LLVM Rust: targets Emscripten Perl: port of (micro)perl-5.16.3 Python – port of CPython Ruby – port of Ruby

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Asm.js

Start with the simplest possible case. Write down what Asm.js claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Asm.js before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Asm.js ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Asm.js

In research
Asm.js appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Asm.js in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Asm.js is common in secondary-school and first-year university syllabi. It links to neighbouring topics JavaScript, Mozilla, Web programming, so understanding it makes those chapters shorter.
In everyday life
Look for Asm.js outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Asm.js” →

How to study Asm.js in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Asm.js means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Asm.js out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Asm.js in simple terms?

asm.js is a subset of JavaScript designed to allow computer software written in languages such as C to be run as web applications while maintaining performance characteristics considerably better than standard JavaScript, which is the typical language used for such applications. asm.js consists of…

Why does Asm.js matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Asm.js?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Asm.js.

Tags

  • JavaScript
  • Mozilla
  • Web programming

Keep exploring