Asm.js Gets Faster
mikejuk writes "Asm.js is a subset of standard JavaScript that is simple enough for JavaScript engines to optimize. Now Mozilla claims that with some new improvements it is at worst only 1.5 times slower than native code. How and why? The problem with JavaScript as an assembly language is that it doesn't support the range of datatypes that are needed for optimization. This is good for human programmers because they can simply use a numeric variable and not worry about the difference between int, int32, float, float32 or float64. JavaScript always uses float64 and this provides maximum precision, but not always maximum efficiency. The big single improvement that Mozilla has made to its SpiderMonkey engine is to add a float32 numeric type to asm.js. This allows the translation of float32 arithmetic in a C/C++ program directly into float32 arithmetic in asm.js. This is also backed up by an earlier float32 optimization introduced into Firefox that benefits JavaScript more generally. Benchmarks show that firefox f32 i.e. with the float32 type is still nearly always slower than native code, it is now approaching the typical speed range of native code. Mozilla thinks this isn't the last speed improvement they can squeeze from JavaScript. So who needs native code now?"
Umm, anyone who wants their code to not run substantially slower. Seriously, do you front end programmers really think nobody does numerical simulations or other performance-sensitive work? In my line of work, I'd kill for the opportunity to make my code 1.5 times faster!
Part of what I do is writing high performance code both in "native" and various scripting languages. I just completed yet another comparison for our product, looking for the best performing scripting language for a specific task. Lua with a "just in time" compiler came in first. It is *only* about x10 slower than native code produced by gcc with moderate optimization, when measured on variety of numeric benchmarks. It is considerably slower when dealing with strings and more complex data types, as expected. All other tested scripting engines were considerably behind. I'll admit that javascript was not tested, and promise to try it out. That said, a claim of "being only 1.5 times slower than native code" is something I haven't seen *any* non-native environment be able to achieve in the last 20 years or so.
Update the ecma standard to include a set of features that is meant to be used when compiling a better language (C#, C++, Java, Perl, Python, LISP, Haskell, x86 asm, MUMPS, pig-latin, etc) down to javascript so it will run in any browser at native code speed. Then I think everyone will be happy.
So who needs native code now?
For a start, anyone implementing a JavaScript engine.
In the '90s, Microsoft's distributed component platform was DCOM, and they pitched Visual Basic as the 4GL language for custom applications. So there were lots of VB developers trying to develop and use COM components. Trouble was, COM was supposed to be programming language-neutral, but the entire architecture was designed with C/C++ in mind. So VB components kinda, sorta worked, with a lot of workarounds and gotchas (for example: dual interfaces are a cool feature of COM, right? But not if you plan to support VB clients). A pretty sharp MS MVP guy named Ted Pattison wrote some books about how you could maybe get this to work.
Why do people even bother with this kind of garbage when they need high performance? Use a language that was designed for it from the get-go, with direct access to the memory heap and system calls.
The idea of compiling to JavaScript has been done a lot. Microsoft Labs had a project to take CLR codes and compile down to JavaScript. It was abandoned as too slow. I'm sure improvements to the JavaScript engines have made it more feasible. But, as noted the lack of certain native types and immutable data types (i.e. DateTime) forces a ton of static analysis to be done just to figure out that hey, that variable could be an plain integer. And it has to be conservative. Much easier to just have integers and be done with it.
If there is such an insistence on making the web an application platform for everything, then I think at some point you just have to standardize on a VM. Yes, yet another one. So, you can use Dart, JavaScript, Scheme, C#, Java, whatever there's a compiler for. Treat the DOM as core API and enjoy.
Personally, I just hope people realize that operating systems have been doing this well for years, that sandboxing isn't unique to web browsers and that "native" applications are actually a good thing.
The mobile app thing gives me a bit of hope, but it's sad that people seem unwilling to download a installer from the web from a trusted source and install it. I find it a bit strange that people are turning to the web to solve a problem that the web greatly expanded the widespread propagation of viruses and other malware.
And what people are surprised by is a bit baffling as well. A web browser isn't magic. Being impressed that you can run Doom on a modern web browser is missing the forest for a tree. I could run Doom on my computer for ages now. Me having to visit a URL to do so isn't not a major actual change nor improvement, despite the technical accomplishments that went into it.
Anybody who writes performance-sensitive code other than trivial contrived benchmarks.
Take a compact binary encoding of a CPU instruction set, convert it to text, run it through gzip, ungzip it, translate it back to binary instructions for a CPU.
Am I the only one who is wondering what the hell is going on? Distribute a binary already!
Since asm.js isn't sugared predication, there isn't a natural formalism to make assertions (declarations) about variables such as the set of values they can take on. Not all of these assertions need to be checked at run time. Some can be used at compile time to infer the most efficient implementation for certain use cases, and even generate different implementations for different use cases.
Seastead this.
JavaScript always uses float64 and this provides maximum precision, but not always maximum efficiency.
That doesn't make too much sense to me, I thought that typed arrays have always had 32-bit floats as an option. Why should 32-bit storage (on-heap typed arrays) and 64-bit registers (scalar values) be significantly slower than 32-bit storage and 32-bit registers? I thought the performance discrepancy between singles and doubles in CPU float units disappeared a decade and a half ago? (Or are simply single-to-double loads and double-to-single stores somehow significantly slower?)
Ezekiel 23:20
Yet another one who thinks Java has anything at all to do with Javascript.
The person writing the javascript engine.
Unless it's implemented in asm.js
It's JavaScript engines all the way down, man...
In theory, the text can be translated to x86, ARM, or any other 32- or 64-bit architecture, and the translator enforces some memory safety guarantees.
Actually this has nothing to do with javascript.
ASM.js is a java VM like technology which compilies code in C++ and Java to javascript which then uses a JIT compiler.
This could be very usefull for things like Citrix or crappy activeX like functions being made available to mobile devices and browsers. Useful yes, but like java and activeX a big fucking security hole if not done right.
I think ASM.JS is dead out of the water as Google refuses to support it. Google wants you to use Dart and Google Chrome apps that run near native speed and require a Google ecosystem. MS wont support it either as it gets rid of the dependancy for Windows and crappy Metro apps.
I think Google is turning more into the IE of the late 1990s as it is becoming so standard now.
http://saveie6.com/
More 32-bit floats will fit in your CPU's data cache than 64-bit floats.
If the (admittedly stupid) last sentence of the submission hadn't been present, would you still be complaining? They're improving the speed of js by doing some interesting stuff - what's wrong with that?
Java is not IEEE compliant for floating point calculations anyway. All high performance computing that needs reliability and IEEE compliance uses either C, or specialized asm libraries.
Using Java for numerical analysis is like using a monkey to design a space shuttle.
Funny I thought Java runs on the top mainframes at the Stock Market and major institutions world wide.
http://saveie6.com/
If you want threading in Firefox, then go vote up the bugs related to Electrolysis.
I work in a bank. Our end of day processing runs up numbers in the trillions. Individual currency accounts used for internal processing can easily be in hundreds of billions.
I want a list of atrocities done in your name - Recoil
Until you get into the tens of millions of dollars, 32-bit integers are fine for counting currency down to the cent. What kind of "(monetary) transaction processing system" were you talking about?
Well, let's see, in 2007 there were over 200,000 companies in the U.S. with over $10,000,000 in receipts... (http://www.census.gov/econ/smallbus.html)
Not mainframes, more clusters. All the stuff used for reconciliation and position management uses pure integer and fixed point maths. Market makers like pushing floating point numbers around, but that's just for quick profit calculations when doing the trades. At the end of the day, everything's reconciled with precise maths.
The person who needs to use threads.
Or the person needing access to special functions such as page fault interrupts.
Or the person porting python/Haskell/your favorite language.
If Pandora's box is destined to be opened, *I* want to be the one to open it.
Plus of course, not all currencies have just 2 decimals and some have exchange rates which produce huge numbers such as the Yen.
I want a list of atrocities done in your name - Recoil
However even IE 8 supports multithreading and having a different thread for each tab. Chrome had this since 1.0. Webworkers require this and security requires this.
Uhm, no, security requires separate processes, not separate threads.
Shit on a 6 core cpu I had for 3 years now I should not have +30 tabs using one damn cpu?!
True, but on the other hand, Flash can only hose 1 core and the performance of the browser, leaving all other processes able to share 5/6 of your CPU power. With process-per-tab, now Flash can hose all your cores and bring the whole box to its knees.
Emscriptem is a technology that compiles code in C++ to javascript. Asm.js is a restricted subset of javascript that is much easier for the compiler to handle.
Google wants you to use quite a few different things, probably the 'best' is NaCl (native client) which runs practically native code in a sandboxed runtime.
I get pissed when you hear programmers say "Oh memory is cheap, we don't need to optimize!" Yes you do. In the server world these days we don't run things on physical hardware usually, we run it in a VM. The less resources a given VM uses, the more VMs we can pack on a system. So if you have some crap code that gobbles up tons of memory that is memory that can't go to other things.
It is seriously like some programmers can't think out of the confines of their own system/setup. They have 16GB of RAM on their desktop so they write some sprawling mess that uses 4GB. They don't think this is an issue after all "16GB was super cheap!" Heck, they'll look at a server and see 256GB in it and say "Why are you worried!" I'm worried because your code doesn't get its own 256GB server, it gets to share that with 100, 200, or even more other things. I want to pack in services as efficient as possible.
The less CPU, memory, disk, etc a given program uses, the more a system can do. Conversely, the less powerful a system needs to be. In terms of a single user system, like maybe an end user computer, well it would always be nice is we could make them less powerful because that means less power hungry. If we could make everything run 1.5 times as fast, what that would really mean is we could cut CPU power by that amount and not affect the user experience. That means longer battery life, less heat, less waste, smaller devices, etc, etc.
Except duh, websites are not software, "websites" are an experience made up of literally dozens of components on servers, clients, browsers, intermediate networking equipment, routers, modems, etc that all contain software - only one small piece of which is usually written in Javascript/HTML.
Let's correct that sentence:
"Now Mozilla claims that with some new improvements it is at worst only 1.5 times slower than single threaded, non-vectorized native code."
In other words, it's only 1.5 times slower than native code that you haven't made any serious effort to optimize. Hey, I think it's great they're improving the performance of Javascript. But this is still nowhere close to what you can do in native code when you actually care about performance.
"I'm too busy to research this and form an educated opinion, but I do have time to tell everyone my uninformed opinion."
the linux kernel works on its own? none of that GNU crap or anything else? and VLC only needs QT to run?
Websites are no less than distributed applications. If you had been paying attention you would have noticed that website development has gotten a lot more rigorous than in the old days.
http://www.rootstrikers.org/
I'd wish they'd stopping slowly and painfully going through the intermediate steps and standardize the Javascript Bytecode representation. Then javascript wouldn't be any slower than native code. Even faster in some situations ( due to runtime optimizations, if the Java folks are to be believed ).
Why on earth are we still only transferring Javascript as text? It doesn't really help security. Is obfuscated Javascript any easier to read than decompiled bytecode?
Let's just open up my handy Javascript console in Chrome...
(0.1 + 0.2) == 0.3
false
It doesn't matter how many bits you use in floating point. It is always an approximation. And in base-2 floating point, the above will never be true.
If they're saying that JavaScript is within 1.5x of native code, they're cherry-picking the results. There's a reason why people who care have a rich set of numeric datatypes.
Wait. are you talking to *me* or just making a statement? Because that's pretty much exactly what I said...
No, scripters are still scripters. It's the designers that are getting better at their jobs.
How about making the Canvas tag not suck?
Seriously, you can give it the most simple test (draw an image every frame, or fill a rectangle), and it will run unacceptably slow. For comparison, a simple Win32 program that changes the background color of a maximized window once per frame has very low CPU usage, but a simple Javascript program that flashes a canvas red and blue every other frame uses 100% CPU usage if the canvas is 1024x768.
Until the canvas isn't the bottleneck anymore, Javascript graphical programs will continue to suck, and no amount of ASMJS magic will help with that bottleneck.
Anti-Criminals ... aka real programmers.
The second rule is that often only a very small percentage of the code must be optimized. The rest is not going to run that often, maybe only once.
So if the browser is built to run the things that need to run fast natively. and then take javscript or whatever to run combinations of those things, I don't see what speed disadvantages there are. It should be a matter of a browser implementation. If there are some common numeric tasks that are slow, then that needs to be native. Like matrix calculations for rotations.
"She's a scientist and a lesbian. She's not going to let it slide." Orphan Black
People implementing JavaScript engines. Among many other. You would instantly lament if every bit of machine code deployed in the world were suddenly to reimplement itself in your beloved JavaScript.
Signature intentionally left blank.
The "everything must be a web site" mentality has confused the hell out of me for ages as well.
For some time I assumed it was probably for portability, but while writing a game I'm sure no one cares about I found that portability actually isn't all that difficult. The number of #ifdef involved to make it compile for both Windows and Linux (and FreeBSD, though I haven't compiled for it in about a year) amount to a very minor part of the code, and probably wouldn't have been even that much if I were more of a fan of using other people's libraries. (It does use GLFW, which no doubt is taking care of at least 95% of the portability issues for me.) I suspect even a Mac OS version wouldn't be too difficult though I never could figure out how to produce one from Linux. Granted, I don't have a reliable Linux version available due to LGPL issues, but no one cares about Linux anyway. They could certainly achieve portability between the platforms they care about (anything Windows runs on, and Mac OS) easily enough with a compiled C program, and if they really cared they could also set up a server farm of Linux machines so that they have a copy of each distribution available to compile a Linux version too.
So then why is it I go to YouTube and deal with shitty web video that barely plays correctly if it does at all? I've seen the red and blue channels reversed in video, I've seen 30-second seek times (even seeking backwards), and at times it simply doesn't work at all. The same goes for hulu.com or any online radio web site. Even Facebook could benefit from having a custom application. ...and all of these things would run more efficiently as an application than they do as a web site.
Hell, the new flickr.com is probably the best example. In their desire to have a wall of images in the search results, the javascript spends so much time examining possible arrangements of the images that the computer grinds to a halt. The web site is damn-near unusable, and certainly only "usable" if you're desperate. As far as "I'm bored so I'll look for something interesting on Flickr" goes, it's positively worse than being bored. Granted, they should do the intelligent thing and just go back to the web site they had, but as long as they're insistent on styling their image indexes like that, if they were to do it in a custom application, it could figure out the arrangements it wants in the blink of an eye and so the user experience wouldn't be so miserable.
The craziest thing about it for me is that, given the choice between making a web site and making a proper application, I'd rather make a proper application. Communicating with a server over a TCP connection is a piece of cake, so I don't need a web browser for that, nor do I want to structure everything as an HTTP request anyway. I also don't have to worry about cross-testing in every version of every web browser. Granted, you do then have to cross-test to different operating systems, but at least between Windows and Linux, I've found most of the issues that crop up tend to be data access errors that show up in one version but not the other simply due to data being arranged differently in each executable, and I haven't even seen that since compiling with -fstack-protector-all and writing a wrapper for malloc() in an effort to catch any invalid data access. With web sites I always seem to spend at least 20% of my time trying to make things look at least reasonable in every web browser, and I'm not one of those people who even tries to do fancy shit, I just keep running issues into things like one web browser inserting a blank line somewhere where another doesn't and trying to figure out some way to structure everything so that, if they can't all look the same, then at least none of them end up with ass-ugly formatting.
So I can only guess that the "everything must be a web site" is a user-driven demand.
Part of that might be the Windows convention that no matter how trivial
I was referring to you. Pardon me if I misconstrued your meaning. Since websites/applications ARE software I am not sure what you meant.
http://www.rootstrikers.org/
Instead of overgeneralizing, let's specify the subject. As was said above, "scripting isn't programming". This ignores many of the huge libraries written in scripts, such as ASM.js, which were nowhere near trivial to crap out.
A few lines of scripting isn't programming, but it qualifies as "website development". Also included is a fully separated 3-tier, pre-compiled architecture with persistence and unit tests.
Websites are frequently, way less than distributed applications. Speaking of websites as if they encompass only a part of what's true speaks of the ignorance of your experience. And I would have replied to Dahamma above but you got the mod points.
I don't see any proof of rigor in anything I have seen - I do see PHP and python, C# and jsp - all both with and without any rigor. Being compiled does not imply rigor, and being separated across N tiers does not mean rigor. I'm pretty sure you mis-typed, but the information here being general is, in general, completely without factual basis.
In fact, I would argue that more people are writing websites via programming, where they used to do the same via scripts, and with the exact same regard (or lack of) to rigor.
Performance sensitive is not the issue here. The argument against JavaScript has always been no because it's slow. This at least opens the door for options other than Silverlight, ActiveX, Applets, and whatever other binary junk people want to run in my browser.
If you even considered for one minute running number crunching like Seti@home or other distributed computing, or bitcoin mining, or anything else that would tax your cooling and cost you energy, in JavaScript, sign yourself up for the looney bin.
In fact, just preface this to the headline and it really makes a lot of sense: "For all you whiny developers who insist on using proprietary plugins because scripts are SOOO slow - look at these results and ask yourself who needs native code now?"
Several people answered this question as if it were addressed to everything ever in the history of computers. You don't need native code because js is just as fast, so NOTHING you do will ever need native code. Is that how you read it? And you think it is a legitimate question? Won't a JavaScript implementation need native code? A BIOS?
Clearly there is a need - so there must be more to the question, like audience and context.
Exactly, which is why PNaCl is superior at the end of the day. Good luck trying to jam multi-threaded, shared memory programming into JS. The standards committee itself already hate JS (but they maintain it for the "good of the web"). Don't even think about getting the standards committee to even think about a shared memory (webworkers with ArrayObject ownership transfer is as much of a concession as anyone's ever going to get). All JS VMs will have to be written from scratch just to support this programming model.
Yes it is, see strictfp
If you don't specify strictfp, the JVM is allowed to use higher precision data types in intermediate calculations, leading to greater precision and better performance at the expense of IEEE conformity.
If you do, you get IEEE 754 compliance.
Asm.js uses ahead-of-time compilation, not just-in-time.
You have to price out all the details. The question isn't what the server costs to buy. It is what it costs to buy, what support on it costs, both in terms of a warranty and sysadmin time, what physical space costs, or is available, what power and cooling cost, and what kind of reliability you need.
A "cheap" server can end up being not so cheap in many cases. A cheap server is great right up until the point where it fails, and then there is no way to fix it or restore it in a reasonable amount of time.
You can see what it costs for more realistically reliable things by looking at what VMs cost on the open market. For example Pair, who provides top notch reliability and support, wants about $1500/year for a server with 3GB RAM/80GB disk. Linode, which is much lower end particularly with regards to management, but still solid, wants about $500/year for 2GB RAM/96GB disk. These are for virtual servers, not physical systems. That gives you some idea what a server might actually cost in terms of all the things like power, cooling, bandwidth, maintenance, management, hardware refresh, backup, and so on.
That aside, if the program is for a very specific task, ok then development cost can be looked at fairly straight to server cost 1:1, if it is for resale/use elsewhere, then not so much. You can't very well try and foist off the development cost to each customer or argue they should be willing to buy a lot of hardware to support it.
I'm not saying programmers should spend man-years optimizing for every little fractional ounce of improvement (though in some cases it is worth it, read up on some of the stuff Michael Abrash has done) but that optimization does matter.
Jane Street runs OCaml! :-)
Threading has nothing to do with security.
Running entirely difference processes for each tab is done for stability.
That is one of the points of asm.js asm.js a bytecode for other languages. You can use any language you want.
Some people even port the runtime from an other scripting language, like Python.
New things are always on the horizon
What are the chances that this will be integrated in the Linux kernel ? For better performances as a host and as a guest. I can already see large cluster of Linux running in heterogeneous environments.
Well, let's see, in 2007 there were over 200,000 companies in the U.S. with over $10,000,000 in receipts... (http://www.census.gov/econ/smallbus.html)
Did any of them declare losses of -$2147483648 to the IRS?
systemd is Roko's Basilisk.
Did any of them declare losses of -$2147483648 to the IRS?
Ahem, -$21,474,836.48? Yes, certainly. And many more reported total revenue of that or more.
(Yes, you get to round to dollars for the IRS, but no that does not mean your accounting system can ignore cents.)
If you are writing assembly in javascript you are doing it wrong. A web browser is supposed to display documents, not run desktop applications.
If you are writing desktop applications (ie. a windows PC) for a web browser, then yes, you are doing it wrong. "who needs native code" is certainly over-stating things. HTML + Javascript is appealing when you want to write an application - what smartphones call "apps - that runs on as many platforms - architectures, operating systems, screen sizes - as possible without rewriting all of your code 300 times for each device. If you can write your main code once and have it run on your xbox, playstation, samsung S4, blackberry, iphone, PC, linux, mac, your refrigerator and barbeque then why wouldn't you do that! And with html + javascript there is no installation required - just send a mass email with a shortcut to the webpage for everyone that needs it. It's more like "who needs 300 different versions of the same native code"
If you really need to process dynamic data with that level of control and efficiency with the result put on a dynamic web page, then you need a daemon or some service application doing it, and have JS pull the result. NOT make JS do the entire damn thing.
so you are saying you should spend money beefing up your server so you can present info from the client, to that very same client, after doing some calculations it could have done itself? If the client already has a system capable of doing its share of the work, then why not distribute the load and enable it to do so?!
Used correctly, 250% the original length of time. Next!
Unity? Screw that: XFCE. Slashdot Beta? Screw that: SoylentNews. Australis? Screw that: Pale Moon. UX developers DIAF
Well, I guess it may be splitting hairs... but IMO a "website" is not necessarily "software" any more than a house is "wood" or "plumbing". I can have a "website" that is made up entirely of static HTML and images - or as its simplest, just a single image. And no, I don't think HTML qualifies as software, as it really doesn't fit the common definition of "instructions that direct the operation of hardware"; HTML is structured data, not code.
Anyway, my point in replying to the OP is that even the silliest static website (let alone some Javacript-based client) would not work without the dozens of *native* (or Java-based servers, whatever) software components that are required to realize it in your web browser on your screen.
It's not Java-based, obviously, but it's origins *were* definitely as a scripting language. It was basically designed to automate the creation of HTML documents/fragments on the client by embedding interpreted code. Now, of course, it does a lot more than would be described strictly as a "scripting language", but colloquially most people now equate dynamic interpreted languages with scripting, anyway...
What are you talking about? JavaScript must always necessarily be slower than native code due to its abstracted nature. If it needs an interpreter, or virtual machine, or any other intermediate process between the program code and the CPU, there will be overhead.
Having the CPU execute native code directly will always be faster.
Now, there's an argument to be made that a higher-level, more abstracted language makes people more productive, and by extension, program code more expressive. But this is a different trade-off and objective than building a high-performance application.
-dZ.
Carol vs. Ghost
Is not that it is slow (although it is..) it's that it sucks. It doesn't allow good coding practices, let alone enforce them.
You've been modded flamebait, but there is some truth in what you say. I'd argue, however, that it's not that JavaScript doesn't allow good coding practices -- it does -- but that it does nothing to encourage them, and even discourages them in some cases.
You can write good JavaScript code if you know how and use some discipline. In his book JavaScript: The Good Parts, Douglas Crockford encourages developers to use only a subset of JavaScript's features. The subset he recommends isn't as strict as Asm.js, but he isn't afraid to admit that some features of JavaScript are just poorly designed and shouldn't be there -- so if you want to write good JavaScript code, you should ignore them.
I recommend the book. It's a quick read. It doesn't aim to be a tutorial or a comprehensive bible of correct JavaScript practice. At the very least, though, anybody who works with JavaScript will probably come away from it having seen a new perspective on how the language works and how one should approach it.
Breakfast served all day!
I think ASM.JS is dead out of the water as Google refuses to support it.
No, that's not true. You maybe have things backwards. Mozilla refuses to support NaCl, which is Google's similar-goal-different-approach technology.
Breakfast served all day!
What are you talking about? JavaScript must always necessarily be slower than native code due to its abstracted nature. If it needs an interpreter, or virtual machine, or any other intermediate process between the program code and the CPU, there will be overhead.
This is kind of an old-fashioned argument. Modern VMs are often essentially executing native code by the time the code is actually running. If the bulk of the overhead happens at launch time, or a JIT compiler only has to step in every so often, the level of performance can be such that the difference from "pure" native code is insignificant for most applications. Don't mistake a modern VM for a 1980s style Basic interpreter. The two are very different beasts.
Breakfast served all day!