Feeds

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

New tech promises browser apps at near native speed

Reducing security risks from open source software

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 Power of One eBook: Top reasons to choose HP BladeSystem

More from The Register

next story
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
Captain Kirk sets phaser to SLAUGHTER after trying new Facebook app
William Shatner less-than-impressed by Zuck's celebrity-only app
Apple fanbois SCREAM as update BRICKS their Macbook Airs
Ragegasm spills over as firmware upgrade kills machines
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
Mozilla fixes CRITICAL security holes in Firefox, urges v31 upgrade
Misc memory hazards 'could be exploited' - and guess what, one's a Javascript vuln
Put down that Oracle database patch: It could cost $23,000 per CPU
On-by-default INMEMORY tech a boon for developers ... as long as they can afford it
Google shows off new Chrome OS look
Athena springs full-grown from Chromium project's head
prev story

Whitepapers

Top three mobile application threats
Prevent sensitive data leakage over insecure channels or stolen mobile devices.
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.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.
Designing a Defense for Mobile Applications
Learn about the various considerations for defending mobile applications - from the application architecture itself to the myriad testing technologies.
Build a business case: developing custom apps
Learn how to maximize the value of custom applications by accelerating and simplifying their development.