Digging into JavaScript Performance, Part 2

UPDATE. After I posted these numbers, Alon Zakai, Emscripten’s author, pointed out options for generating optimized JavaScript. I reran my benchmarks; check out the updated table below and the script used to generate the new results.

At the beginning of the year, I tried to justify my claim that JavaScript has a long way to go before it can compete with the performance of native code.

Well, 10 months have passed. WebGL is catching on, Native Client has been launched, Unreal Engine 3 targets Flash 11, and Crytek has announced they might target Flash 11 too. Exciting times!

On the GPU front, we’re in a good place. With WebGL, iOS, and Flash 11 all roughly exposing shader model 2.0, it’s not a ton of work to target all of the above. Even on the desktop you can’t assume higher than shader model 2.0: the Intel GMA 950 is still at the top.

However, shader model 2.0 isn’t general enough to offload all of your compute-intensive workloads to the GPU. With 16 vertex attributes and no vertex texture fetch, you simply can’t get enough data into your vertex shaders do to everything you need, e.g. blending morph targets.

Thus, for the foreseeable future, we’ll need to write fast CPU code that can run on the web, mobile devices, and the desktop. Today, that means at least JavaScript and a native language like C++. And, because Microsoft has not implemented WebGL, the Firefox and Chrome WebGL blacklists are so strict, and no major browsers fall back on software, you probably care about targeting Flash 11 too. (It does have a software fallback!) If you care about Flash 11, then your code had better target ActionScript 3 / AVM2 too.

How can we target native platforms, the web, and Flash at the same time?

Native platforms are easy: C++ is well-supported on Windows, Mac, iOS, and Android. SSE2 is ubiquitous on x86, ARM NEON is widely available, and both have high-quality intrinsics-based implementations.

As for Flash… I’m just counting on Adobe Alchemy to ship.

On the web, you have two choices. Write your code in C++ and cross-compile it to JavaScript with Emscripten or write it in JavaScript and run via your native JavaScript engine. Ideally, cross-compiling C++ to JS via Emscripten would be as fast as writing your code in JavaScript. If it is, then targeting all platforms is easy: just use C++ and the browsers will do as well as they would with native JavaScript.

Over the last two evenings, while weathering a dust storm, I set about updating my skeletal animation benchmark results: for math-heavy code, how does JavaScript compare to C++ today? And how does Emscripten compare to hand-written JavaScript?

If you’d like, take a look at the raw results.

Language Compiler Variant Vertex Rate Slowdown
C++ clang 2.9 SSE 101580000 1
C++ gcc 4.2 SSE 96420454 1.05
C++ gcc 4.2 scalar 63355501 1.6
C++ clang 2.9 scalar 62928175 1.61
JavaScript Chrome 15 untyped 10210000 9.95
JavaScript Firefox 7 typed arrays 8401598 12.1
JavaScript Chrome 15 typed arrays 5790000 17.5
Emscripten Chrome 15 scalar 5184815 19.6
JavaScript Firefox 7 untyped 5104895 19.9
JavaScript Firefox 9a2 untyped 2005988 50.6
JavaScript Firefox 9a2 typed arrays 1932271 52.6
Emscripten Firefox 9a2 scalar 734126 138
Emscripten Firefox 7 scalar 729270 139


  • JavaScript is still a factor of 10-20 away from well-written native code. Adding SIMD support to JavaScript will help, but obviously that’s not the whole story…
  • It’s bizarre that Chrome and Firefox disagree on whether typed arrays or not are faster.
  • Firefox 9 clearly has performance issues that need to be worked out. I wanted to benchmark its type inference capabilities.
  • Emscripten… ouch :( I wish it were even comparable to hand-written JavaScript, but it’s another factor of 10-20 slower…
  • Emscripten on Chrome 15 is within a factor of two of hand-written JavaScript. I think that means you can target all platforms with C++, because hand-written JavaScript won’t be that much faster than cross-compiled C++.
  • Emscripten on Firefox 7 and 9 still has issues, but Alon Zakai informs me that the trunk version of SpiderMonkey is much faster.

In the future, I’d love to run the same test on Flash 11 / Alchemy and Native Client but the former hasn’t shipped and the latter remains a small market.

One final note: it’s very possible my test methodology is screwed up, my benchmarks are wrong, or I suck at copy/pasting numbers. Science should be reproducible: please try to reproduce these results yourself!

The Completionist’s Guide to Sims 3 iPhone

I play simulation games in two phases: first, I tackle the often-repetitive mechanics, unlocking every option and building up money so that I can creatively decorate my house or farm or whatever.

Sims 3 for iPhone was no different. Having accomplished nearly everything in the game, I will share a strategy for completing all 73 goals and acquiring the best job: $600 per day at the Pawn Shop.

The Objective

To open the pawn shop, you must complete the following 73 goals:

Goals for all Sims (55)

  • Try fishing
  • Try cooking
  • Buy fishing kit
  • Buy watering can
  • Buy repair kit
  • Buy a stove
  • Buy a bath
  • Gain a skill point at cooking
  • Gain a skill point at fishing
  • Gain a skill point at repairing
  • Meet a new Sim
  • Befriend a Sim
  • Begin a romantic relationship
  • Make an enemy
  • Make a Sim laugh
  • Annoy a Sim
  • Insult a Sim
  • Creep-out another Sim
  • Slap a Sim
  • Get a job
  • Buy something
  • Catch a fish
  • Catch a trout
  • Catch a salmon
  • Catch a catfish
  • Repair something
  • Discover a new recipe
  • Cook something
  • Cook grilled cheese
  • Cook steak & veggies
  • Cook minestrone
  • Grow something
  • Grow carrots
  • Grow corn
  • Grow tomato
  • Watch TV
  • Kick over a Trash Can
  • Sleep in another Sim’s bed
  • Use another Sim’s shower
  • Use another Sim’s toilet
  • Get a better couch
  • Get a better TV
  • Accumulate $1000
  • Catch 15 fish
  • Harvest 30 crops
  • Stay entertained for three days
  • Stay fed for three days
  • Stay rested for three days
  • Stay clean for three days
  • Meet all the Sims in town
  • Make a Sim jealous of you
  • Sleep in three other Sim’s beds
  • WooHoo with someone
  • Get a promotion
  • Reach the top of the career ladder

Maniac Personal Goals (4)

  • Use everyone’s toilet at least once
  • Use everyone’s shower at least once
  • Creep out five people
  • Watch three people sleeping

Sleaze Personal Goals (2)

  • Be romantically involved with three Sims
  • WooHoo eight times in one day

Power Seeker Personal Goals (3)

  • Accumulate $5000
  • Own the best house
  • Own the best TV, stereo and stove

Nice Guy Personal Goals (2)

  • Get married
  • Be liked by all the Sims in town

Jerk Personal Goals (4)

  • Be disliked by all the Sims in town
  • Slap four people
  • Insult five people
  • Kick over all the trash cans in town

Jack of All Trades Personal Goals (3)

  • Achieve the highest fishing skill level
  • Achieve the highest repairing skill level
  • Achieve the highest cooking skill level

There are six Sim classes:

  • Jack of All Trades
  • Nice Guy
  • Jerk
  • Sleaze
  • Power Seeker
  • Maniac

As previously mentioned, you will need to complete every Goal to unlock the Pawn Shop. Of the 73 total Goals, 55 randomly appear as Wishes, no matter which Sim class you choose, and they’re generally easy to complete.

On the other hand, each Sim’s Personal Goals are relatively difficult. You’ll need to create at least six Sims, one of each type. For the optimal path through the game, you should complete their personal goals from hardest to easiest. I will give an efficient play sequence below.

Play Order

Jack of All Trades

Create six Sims, and designate one as your “main” Sim. Your main Sim is the one with which you most identify.

Your main Sim is a Jack of All Trades (you’ll later need its Repair skill at the Pawn Shop). Play him or her first, building up skill levels as rapidly as possible. Learn to fish, because fishing is the best way to make money. As you accumulate money, buy as many of the cheapest stereos you can afford, filling your house. Each time you enter the house to eat or rest, turn them all on. Eventually, they will start breaking down, allowing you to practice repairing.

Practicing cooking is easy – simply buy bread, cheese, and the Grilled Cheese recipe and make it over and over again.

Once fishing, repairing, and cooking are level 5, save, quit and move to the next Sim.

Power Seeker

Your second Sim is your Power Seeker. Fish until you have $5000 and tool around town until you have the option to upgrade your house twice and buy the most expensive TV, stereo, and stove. Furniture and home upgrades simply take time. This is a good opportunity to accumulate general Goals.

At this point, you should have most of the 55 general Goals.

The Other Sim Types

Sleaze, Nice Guy, Jerk, and Maniac have fairly easy Personal Goals. The order in which you complete them does not matter.

At this point, you should have almost all of the Personal Goals. If not, keep playing — they’ll eventually show up.

Goal Tips

If you need the “Buy XXX” goal but you already have XXX, then try selling it. Eventually the Goal should appear as a Wish. Lock it in and repurchase the item.

If you need the “Gain a skill point at XXX” goal and you’re maxed out, switch to a Sim that is not maxed out.

If you’re having trouble Meeting all Sims, double-check all of the houses at different times of the day. I’ve noticed Bernie can be hard to find. Most Sims are sleeping in the middle of the night – try breaking into their houses, waking them up, and meeting them.

If you need to befriend a Sim but you’re already friends with all of the Sims, start a new Sim or insult a Sim until it becomes an enemy, then befriend him or her.

The best way to annoy, insult, and creep-out other Sims is to find a house with two Sims (e.g. Marcell and Theresa), barge in, and start flirting with one of them. Both will be creeped out and annoyed, and the flirting will insult the other and make him or her jealous. If that doesn’t work, start using their toilet, shower, and fridge.

Tips and Tricks

Fishing is the best way to make money. Each tuna sells at QuickMart for $100, and salmon for $65.

Besides the Pawn Shop, working at the Town Hall is the best. It’s easy to get promotions (both Ruth and the Town Hall are nearby) and you just need to be friends with everyone in town. As Vice President, you can make $300 per day.

Creating romantic relationships is easy. It sounds rude, but keep pestering the Sim with Flirt, Tender Embrace, Hot Smooch, and WooHoo. You should be able marry a Sim within two conversations.

Too many WooHoos and your Sim will die! Don’t overdo it in the bedroom!

If you get married, give your spouse a phone call and invite him or her over. Once your spouse arrives, you can invite him or her to move in.

Since the Town Hall is right by your house, it’s the best job until the Pawn Shop is open.

The Sims

  • Nina
  • Ruth
  • Johnny
  • Kia
  • Jake
  • Maggie
  • Jack
  • Jill
  • Walter
  • Luke
  • Bernie
  • Marcell
  • Theresa

The Jobs

Town Hall (boss: Ruth)

  • Campaign Intern ($100/day) M-F 8:30-18:00
  • City Council Member ($150/day) M-F 8:30-18:00
  • Local Representative ($200/day) M-F 9:00-18:00
  • Mayor ($250/day) M-F 10:00-18:00
  • Vice President ($300/day) M-F 10:00-17:00

Corsican Bistro (boss: Marcell)

  • Kitchen Scullion ($100/day) M-F 10:30-18:30
  • Ingredient Taster ($150/day) M-F 10:30-18:30
  • Line Chef ($200/day) Tue-Sat 11:00-17:00
  • Sous-Chef ($250/day) Tue-Sat 11:00-17:00
  • Chef de Cuisine ($300/day) Tue-Fri 11:00-17:00

Laboratory (boss: Kia)

  • Test Subject ($100/day) M-F 9:00-16:00
  • Lab Tech ($150/day) M-F 09:00-16:00
  • Fertilizer Analyst ($200/day) M-F 09:00-16:00
  • Carnivorous Plant Tender ($250/day) M-F 09:00-16:00
  • Genetic Resequencer ($300/day) M-F 10:00-16:00

Quickmart (boss: Bernie)

  • Filing Clerk ($100/day) M-F 08:30-18:30
  • It’s not worth getting promotions at Quickmart. Bernie is a dick and you have to work there FOREVER before he’ll promote you.

Pawn Shop (boss: Nina)

  • Con Artist ($300/day) M-F 16:30-23:55
  • Safecracker ($400/day) M-F 16:30-23:55
  • Cat Burglar ($500/day) M-F 17:00-23:55
  • Master Thief ($600/day) M-F 17:00-23:00

In the End

With the information given above, you should have no problem achieving 73 goals and as much money as you need to decorate your dream home.

If you get stuck at any point, feel free to leave a comment on this page and I’ll update the document.

Why are old, but popular, games so hard to find?

Back in 2002, when I was in college, I was walking down the street from one of my classes, and a friend randomly walked up to me and handed me a video game. It had a shiny box, so I accepted it, but for whatever reason did not even try playing until recently. The game was Age of Mythology, and as I found out in the last few weeks, it is awesome. It’s so much fun, that I want to recommend it to all of my friends. And there’s where the problem lies. As far as I can tell, you simply can’t buy it anymore. I have looked at all of the major online retailers, but all of them either say temporarily out of stock or unavailable. It’s not like age of mythology is some rare game that you have to look especially hard for — it won several awards in 2002 including “game of the year”. And now it’s just gone.

Earlier this year I had the same problem with We Love Katamari. After trying to buy it from six or seven places that claimed they had it — each time it turned out they had miscounted their inventory — Amazon got a new shipment and I managed to snag a copy before it went out of stock again. Once again, this isn’t some obscure PlayStation RPG, this is a recent and well-received game.

So what’s the deal? Will this trend continue? Am I going to have to purchase every game that I think I might want to play in the future? You would think that digital distribution would solve this problem, and maybe it will eventually, but right now I just want to give somebody some dollars in exchange for a couple plastic discs. Is that too much to ask?

(This entry voice-recognized, courtesy of Dragon NaturallySpeaking. Apologies for any weird wordos.)

Consumer Electronics

Laura and I got a Wii on Sunday morning. (Barely. Luckily, Target got a shipment of 88.) And digital cable today. We already had: DVD player, VCR, TV-out (from my computer), PlayStation 2, N64, and a GameCube (which the Wii will replace soon). So… I’m looking for an 8-way A/V switch. Definitely needs composite video, s-video a nice-to-have. Manual switches are better. We don’t need a full-fledged receiver yet, at least until we get an HDTV.

I can’t find anything. Any recommendations?

Help Me Pick a Game

In the recent past, I have finished Dragon Quest 8, Bahamut Lagoon, and Far Cry, basically flushing my game queue. So I’m looking for advice on what to play next. I’ll give a list of games, but feel free to suggest others. Optimizing for fun divided by number of hours is best. For example, Katamari Damacy was supremely fun and short, giving it a very high enjoyment quotient. Here are the games, in no particular order:

The Main List

  • Half-Life 2
  • Zelda: Twilight Princess
  • Final Fantasy XII
  • Rogue Galaxy
  • We Love Katamari
  • Shadow of the Colossus
  • Chrono Cross

The Secondary List

  • Age of Mythology
  • Final Fantasy III
  • Rudra no Hihou (Treasure of the Rudras)
  • Devil May Cry 3
  • Ico
  • Psychonauts
  • Metroid Prime
  • Kingdom Hearts
  • Phantom Brave
  • God of War
  • Lego Star Wars
  • Xenogears
  • Valkyrie Profile (if playing it before VP2 is important)
  • Zelda: Majora’s Mask

Help! (But there are always “eternal games”, like Guild Wars and Gran Turismo that will consume all time in the meanwhile.)

Update: Added Shadow of the Colossus, Chrono Cross, Ico, Metroid Prime, Xenogears, and Majora’s Mask.

GT4 Sucks.

Here’s why:

  • The infamous screen jittering bug.
  • TCS and ASM are disabled in 2-player if you select a car from your garage. Supposedly there’s a bug where you can pick a standard car and then pick one of yours and you’ll get the driving aids back.
  • They totally limited what cars you can take onto dirt tracks.
  • The penalty system is lame. Even still, you can’t enable it in 2-player! So it’s still the fastest to ride the wall at high speed.
  • The penalty system is lame. The other cars can hit you and give you the penalty.
  • The AI sucks. I can win races worth 127 points and the AI can barely win B-spec races worth 50. It doesn’t grasp exit speed at all.
  • Apparently there are pretty serious bugs with the speed limiter in pit. I heard a story about a guy going into pit mid-endurance and it never took the limiter off. Ouch. I’ll confirm once I unlock the endurance races.
  • Why the heck can’t you race on every track in 2-player mode? Some of them would be really fun!
  • The driving missions go from braindead easy to extremely difficult just like that.
  • If you’re using the Logitech Driving Force wheel in a very fast race car on a bumpy track like Circuit de la Sarthe you can barely hang on the wheel. The force feedback really goes nuts. I get the feeling I’ll be switching to a controller later in the game.
  • The audio levels are really bad. Some cars are way louder than others in 2-player, giving that player an advantage. It’s harder to know when to shift if you can’t hear your engine.
  • The difference in volume between being inside your car and outside is pretty drastic. Makes the replays seem kinda… boring.
  • The music is way too quiet, even with the music volume all the way up.
  • It just doesn’t feel like a finished product. I’d give GT3 a 10/10, GT4 a 7/10. Maybe 8.
  • The scrolling marquee text at the bottom of the screen gets old. Miss a sentence? Too bad.
  • I’d like to see more detail in the cars and track (the LOD popping is pretty obvious, and some of the ground textures are hideously blurry) and less detail in the background. Please make the rear-view mirror useful too.
  • It’s not easy (possible?) to temporarily change the ghost settings. And they’re set in the controller options section, per controller…?
  • No ghost in the time trials with the pace car? What’s up with that?

At least the physics are better. And the new courses are fun.

This guy is mostly right.