Feeds

Mozilla: Native code? No, it's JavaScript, only it's BLAZING FAST

New tech promises browser apps at near native speed

Boost IT visibility and business value

The Mozilla Foundation says it has reached an important milestone in the quest to improve JavaScript performance, with some JavaScript benchmarks now running only around 1.5 times slower than when the equivalent algorithms are compiled and executed as native binary code.

"That's a big improvement from earlier this year, when as mentioned before things were closer to 2x slower than native," Mozilla researcher Alon Zakai said in a blog post on Friday.

There is a catch, however. Strictly speaking, the benchmarks Mozilla has tested were implemented not in hand-coded JavaScript, but in Asm.js.

Asm.js is JavaScript. It will run in any browser. But it's only a subset of the full JavaScript language. It intentionally ignores some of the more ambiguous features of JavaScript syntax, leaving a dialect that is more streamlined and – most importantly – easier for runtime engines to optimize for performance.

The idea is that while traditional JavaScript is a syntactically rich, high-level language like C or Python, JavaScript written to the Asm.js spec is closer to assembly language. In fact, much like assembly language, the structure and syntax of Asm.js code is so arcane that application developers are advised not to try to write it by hand. Instead, Asm.js is designed to be output by compilers.

One such compiler is Emscripten, which Zakai and others have developed at Mozilla. Emscripten allows C/C++ source code to be compiled into JavaScript, including Asm.js. It's the tool that makes possible such impressive demos as the in-browser 3D first-person shooter game that Mozilla unveiled last year.

But producing code in Asm.js is only half of the solution. To get the biggest performance gains, explicit support for Asm.js needs to be implemented in JavaScript execution engines.

SpiderMonkey, the JavaScript engine that comes with Mozilla's own Firefox browser, has shipped with Asm.js support enabled by default since Firefox 22 in June. On Friday, Zakai said that continuing improvements to the engine have been largely responsible for the latest Asm.js speed gains.

Specifically, the newest builds of Firefox and Emscripten both include support for instructions that allow SpiderMonkey to run some kinds of floating-point calculations using 32-bit values rather than the typical 64-bit double-precision ones.

Chart showing JavaScript performance gains with Asm.js optimizations

With new optimizations, JavaScript performance is closing in on that of native code (click to enlarge)

According to Zakai, with these optimizations enabled, Asm.js code running in SpiderMonkey can run nearly as fast, and on rare occasions even faster, than native code benchmarks produced by C compilers like clang and gcc.

"Overall, what this shows is that 'native speed' is not a single number, but a range," Zakai wrote. "It looks like asm.js on Firefox is very close to that range – that is, while it's on average slower than clang and gcc, the amount it is slower by is not far off from how much native compilers differ amongst themselves."

So are JavaScript applications everywhere due for a speedup? Not quite. Although support for the new 32-bit optimizations is available in Emscripten now, it's not enabled by default because it increases overall code size and it can actually hurt performance in some cases. Zakai says Mozilla is working on ways to limit these problems.

What the latest findings illustrate, however, is that it is still possible to squeeze more speed out of browser-based applications even as the performance of JavaScript VMs reaches its peak, without resorting to unpopular workarounds like Google's NaCl.

"And even the current performance numbers – 1.5x slower than native, or better – are not the limit of what can be achieved," Zakai writes, "as there are still big improvements either under way or in planning, both in emscripten and in JavaScript engines." ®

The essential guide to IT transformation

More from The Register

next story
Munich considers dumping Linux for ... GULP ... Windows!
Give a penguinista a hug, the Outlook's not good for open source's poster child
The Return of BSOD: Does ANYONE trust Microsoft patches?
Sysadmins, you're either fighting fires or seen as incompetents now
Intel's Raspberry Pi rival Galileo can now run Windows
Behold the Internet of Things. Wintel Things
Microsoft cries UNINSTALL in the wake of Blue Screens of Death™
Cache crash causes contained choloric calamity
Eat up Martha! Microsoft slings handwriting recog into OneNote on Android
Freehand input on non-Windows kit for the first time
Time to move away from Windows 7 ... whoa, whoa, who said anything about Windows 8?
Start migrating now to avoid another XPocalypse – Gartner
You'll find Yoda at the back of every IT conference
The piss always taking is he. Bastard the.
prev story

Whitepapers

5 things you didn’t know about cloud backup
IT departments are embracing cloud backup, but there’s a lot you need to know before choosing a service provider. Learn all the critical things you need to know.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Build a business case: developing custom apps
Learn how to maximize the value of custom applications by accelerating and simplifying their development.
Rethinking backup and recovery in the modern data center
Combining intelligence, operational analytics, and automation to enable efficient, data-driven IT organizations using the HP ABR approach.
Next gen security for virtualised datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.