Feeds

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

New tech promises browser apps at near native speed

Build a business case: developing custom apps

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

Gartner critical capabilities for enterprise endpoint backup

More from The Register

next story
Why has the web gone to hell? Market chaos and HUMAN NATURE
Tim Berners-Lee isn't happy, but we should be
Microsoft boots 1,500 dodgy apps from the Windows Store
DEVELOPERS! DEVELOPERS! DEVELOPERS! Naughty, misleading developers!
'Stop dissing Google or quit': OK, I quit, says Code Club co-founder
And now a message from our sponsors: 'STFU or else'
Apple promises to lift Curse of the Drained iPhone 5 Battery
Have you tried turning it off and...? Never mind, here's a replacement
Mozilla's 'Tiles' ads debut in new Firefox nightlies
You can try turning them off and on again
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
Scratched PC-dispatch patch patched, hatched in batch rematch
Windows security update fixed after triggering blue screens (and screams) of death
This is how I set about making a fortune with my own startup
Would you leave your well-paid job to chase your dream?
prev story

Whitepapers

Top 10 endpoint backup mistakes
Avoid the ten endpoint backup mistakes to ensure that your critical corporate data is protected and end user productivity is improved.
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.
Backing up distributed data
Eliminating the redundant use of bandwidth and storage capacity and application consolidation in the modern data center.
The essential guide to IT transformation
ServiceNow discusses three IT transformations that can help CIOs automate IT services to transform IT and the enterprise
Next gen security for virtualised datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.