Feeds

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

New tech promises browser apps at near native speed

Combat fraud and increase customer satisfaction

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." ®

Combat fraud and increase customer satisfaction

More from The Register

next story
OpenBSD founder wants to bin buggy OpenSSL library, launches fork
One Heartbleed vuln was too many for Theo de Raadt
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Ubuntu 14.04 LTS: Great changes, but sssh don't mention the...
Why HELLO Amazon! You weren't here last time
Got Windows 8.1 Update yet? Get ready for YET ANOTHER ONE – rumor
Leaker claims big release due this fall as Microsoft herds us into the CLOUD
Patch iOS, OS X now: PDFs, JPEGs, URLs, web pages can pwn your kit
Plus: iThings and desktops at risk of NEW SSL attack flaw
Next Windows obsolescence panic is 450 days from … NOW!
The clock is ticking louder for Windows Server 2003 R2 users
Batten down the hatches, Ubuntu 14.04 LTS due in TWO DAYS
Admins dab straining server brows in advance of Trusty Tahr's long-term support landing
Red Hat to ship RHEL 7 release candidate with a taste of container tech
Grab 'near-final' version of next Enterprise Linux next week
Apple inaugurates free OS X beta program for world+dog
Prerelease software now open to anyone, not just developers – as long as you keep quiet
prev story

Whitepapers

Mobile application security study
Download this report to see the alarming realities regarding the sheer number of applications vulnerable to attack, as well as the most common and easily addressable vulnerability errors.
3 Big data security analytics techniques
Applying these Big Data security analytics techniques can help you make your business safer by detecting attacks early, before significant damage is done.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Securing web applications made simple and scalable
In this whitepaper learn how automated security testing can provide a simple and scalable way to protect your web applications.
Combat fraud and increase customer satisfaction
Based on their experience using HP ArcSight Enterprise Security Manager for IT security operations, Finansbank moved to HP ArcSight ESM for fraud management.