The Lua VM, on the Web


This is an experiment in running Lua on the web, by porting the Lua C implementation to JavaScript using Emscripten.


Measure how fast your browser can run a VM in a VM »

Individual Benchmark Results

Benchmark
Result
Scale What it measures
binarytrees
seconds (lower numbers are better) GC performance
scimark
MFLOPS (higher numbers are better) numeric computation performance
warm startup
seconds (lower numbers are better) how long it takes the VM to be ready to run, after it was previously run

FAQ

Are you really porting the entire Lua VM?

Yes: The entire Lua 5.2.4 codebase written in C is compiled to JavaScript here, including a full incremental GC and everything else. It fits in 170K of gzipped JavaScript.

How can a VM running in a VM be fast?

Lua is implemented in portable C. It is possible to run C compiled to JavaScript at speeds approaching that of a native build (using the asm.js subset of JavaScript), which means that you can in principle run C code that happens to implement a VM at high speed as well. Of course this is theoretical until it is actually attempted - that is the point of this project.

What do the numbers here mean?

The individual benchmark results are reported in their native units, and have short descriptions in the table. The final combined score is more abstract, it is basically a normalized mean of the individual numbers, where higher numbers are better. The final score is intended to give a quick summarizing number for comparison purposes.

I got [X] on my machine, how does that compare to other results?

As one datapoint, on a 64-bit Linux machine I get 732 for Firefox Nightly and 454 for Chrome Dev (as of Jan 6, 2014).

Comparing individual results to a native build of Lua, in Firefox the Lua VM runs at 64% of the speed of the native build.

If you want to compare the results to a native build on your machine, you can do so as follows: Grab Lua 5.2.4 (the version compiled here to JavaScript), build it natively, and run the same benchmarks (binarytrees, scimark). To run them, execute lua scimark.lua and lua binarytrees.lua 14.72 (don't forget the second argument in binarytrees).

What about LuaJIT?

This project ports Lua, not LuaJIT. LuaJIT is more challenging to port, because in addition to being a JIT, its interpreter is in optimized handwritten assembly. This is great for speed but makes things trickier when cross-compiling to a new platform.

LuaJIT is significantly faster than Lua, so it is even more significantly faster than this port to the web. However, it's important to remember that even without LuaJIT, Lua is relatively fast compared to most other dynamic languages, like Python and Ruby, that are very useful in many cases even without blazing speed.

How do I build the Lua VM in JavaScript?

Run make emscripten in lua/ in this repo.

Can I use this to write web pages in Lua?

Yes, although lua objects exposed to javascript will never be reclaimed (as javascript does not have finalisers).

For a demonstration, see the REPL and script example pages.

Fork me on GitHub