Optimizing your JavaScript game for Firefox OS
hacks.mozilla.org
hacks.mozilla.org
On Chrome, my sim runs at about 40 ticks/sec on my fancy macbook. Safari and Firefox only get 5-10 ticks/sec.
On an older core2duo HP laptop I see 15 ticks/sec for Chrome and 3-5 ticks/sec for Firefox, on both Linux and Windows.
A new cheap Asus laptop gets numbers very similar to the core2duo, but the rendering works a lot better due to better graphics card.
I want to move some priority queue code I have to handwritten asm.js when I get time, but don't expect it to make a big impact.
Its just so hard to actually profile your code and understand your bottlenecks. What happens in web workers is basically invisible.
Before I had web workers my putting sim ticks into the render callback was making the game very unresponsive, particularly in Firefox.
I dream of the game working on tablets etc, but that seems a long way off.
Games like the original simcity and tycoon series and a-train all worked well on really really low-end machines, at performance that seems out of reach for javascript games that basically work the same :(
The unfortunate reality is that JavaScript, WebGL, and these other technologies running on extremely powerful computers (even if we're talking modern mobile devices) still can't compete with games written back in the 1980s, in C and assembly, which ran perfectly fine on what are now 25-year-old 386 PCs with a mere fraction of the processing capabilities of a modern system.
As an industry, or even as hobbyists, we should not be in this position. At best, we can call the current situation a complete stall in advancement. At worst, but more realistically, it's been a significant step backward.
Today's game written in Javascript will also run on numerous different CPU architectures, on different OSes. This generality means that you can't take advantage of platform specific hacks that allow the eking out of every last scrap of performance from the silicon.
So yes, when you make a game do a lot more work, and you want it to run on multiple platforms, forcing optimisations to be platform-agnostic, you shouldn't be surprised that it's hard to match the performance of 25 years ago. On the other hand, if you use the same technologies as 25 years ago, you'll smash the performance from back then. Likewise, if you try to run SimCity in a browser ported onto a 386 PC, it would be an insult to dogs to say that it runs like a dog...
The screens are bigger, but the CPUs are several orders of magnitude faster ... on paper.
As I specified that it was a webGL game, we understand that actually there's more CPU than ever to focus on the actual sim crunching.
Its a sad observation that there is approaching an order of magnitude performance gap between chrome and FF on this type of Javascript performance, and many orders of magnitude between JS and bare metal.
The web as a platform is terrible, despite the amazing work of browser vendors.
Yet in the same time, x86 processors have gone from 25 MHz to 3,000 MHz, if not higher. That's a difference of 120+ times.
That's not even considering that basically all PCs (including laptops) and many mobile devices, now have CPUs with at least 2 cores, although 4 or 8 cores is pretty typical, too. Back then, uniprocessor PCs were really the only practical option.
The amount of memory available today is significantly larger, too. At the end of the 1980s, it was impressive to have 2 MB of RAM in a desktop PC. These days, we're generally looking at 4,000 MB of RAM, if not more. That's 2,000+ times more RAM today! When it comes to mobile devices, we're still looking at hundreds of times more RAM. The speed of memory today is also much faster than it was back in the 1980s.
And, of course, we've seen absolutely massive strides in video card technology in that same period of time.
Given those kinds of increases in the power and amount of resources that are available now, JavaScript and the other web technologies have absolutely no excuse for performing as poorly as they do.
I don't even buy the argument that JavaScript is much more portable than C was back then. SimCity was ported to many, many platforms. I remember playing it on PCs running DOS, on an Amiga, and even on an SNES at one point.
MS-DOS games in 1992: http://www.youtube.com/watch?v=D6E4FZdY-pM
JavaScript games/tech in 2013: http://playcanvas.com/demos/ http://services.runescape.com/m=itemdb_rs/bestiary/?show=50
[1] https://developers.google.com/chrome-developer-tools/docs/ti...
[2] https://developers.google.com/events/io/sessions/324908972
On the other hand, you're talking about having problems recreating games that ran on 16Mhz CPUs with 1MB of RAM, which makes me somewhat sceptical.
I don't mean to be disparaging, but I expect that there's something in your main game loop that is killing your performance, and that even if you ported it to asm.js or C++ you would still have the same bottlenecks (perhaps masked better on modern hardware by the better native performance).
As a counter-example, a reasonably complete and functional HTML5 port of a game from the 90s: http://www.adityaravishankar.com/projects/games/command-and-...
The tools for finding this stuff suck. The tools for finding and fixing this stuff for just a handful of mainstream browsers - Firefox, Chrome and Safari - suck.
I want Chrome and Firefox to show code hotness inline in their script browser panes of their developer tools. I want profiling to work for web workers. I want all this without needing to compile my own copy of V8 (as Google advocate in the video above).
Just when profiling, electric-fencing and static analysis begin to become mainstream for C++ developers, we're thrown back to basics in browsers.
Just because wanting to see asset load and understand the fold are mainstream profiling problems for people making shopping websites, doesn't mean browsers aren't going to get used more and more for gaming-style workloads and will need the profiling and debugging tools that problem demands.
Given that you said web workers, are you transferring a lot of data between the worker and the document? Web worker transfers are pretty expensive, so it's possible you're seeing the effect of Chrome having a faster implementation.
Does your JS generate a lot of garbage, and cause GC pressure? Chrome's collector handles garbage better than Firefox because it's generational, but getting rid of GC pressure can improve performance everywhere.
I feel your pain about the difficulty of profiling web workers, though. Profiling browsers in general is a pain but it seems like none of the dev tools people on any browser actually bother to make tools work with web workers.
In FF I strongly recommend using the SPS Profiler because it will give you incredibly detailed performance data; however I don't know if it works with web workers either: https://developer.mozilla.org/en-US/docs/Performance/Profili...
My list of JS perf gotchas might contain one or two helpful details for you as well: https://github.com/kevingadd/JSIL/wiki/JavaScript-Performanc...