JavaScript Physics Engine

I follow Scott Hanselman through his blog and Twitter, and he tweeted about a JavaScript physics engine called Box2DJS. This is cool on a couple of levels.

First, it’s a physics engine... in JavaScript. Yeah, that’s frakking sweet (short aside: I hate Battlestar Galactica is just a few episodes away from the series finale!!).

Second, it an excellent test bed for the performance of JavaScript engines in the various browser. The demo on the page has multiple examples. I chose to use the first example for my “testing”. I use that word loosely, as my scientific approach was to create an object every second and count the number of objects before the performance started to wane. My test subjects were IE7, IE8 RC1, Firefox 3.0.6, Firefox 3.1b2, Opera 9.63, Safari 3.2, and Chrome 1.0.154.48. I found Chrome to be the best, which really isn’t surprising because V8 is the best performing JavaScript engine in a shipping browser. I stopped counting at 25, and in fact I could nearly fill the open spaces in the example area before I noticed a slow down. Firefox 3.1b2 came in second with sixteen. Firefox 3.0.6, Safari, and Opera tied at eight. IE8 slowed at #2, and IE7 brought up the rear at #1 (no surprise there).

I can’t think of a good reason to use such an engine in a production environment unless your target platform is Chrome, as it’s useless in IE and next to useless in Firefox, Safari, and Opera. JavaScript execution performance needs an across-the-board boost before a physics engine is practical for the browser. We’re on the way of seeing such boosts thanks to engines with JIT compilers like V8 and TraceMonkey (I’m disappointed in the latter’s performance in this little test).

Still, it is cool to see a physics engine written in JavaScript; even if it is impractical.

2/18/2009 3:42:27 PM | Tags: Internet Explorer, Browsers, JavaScript, Chrome, Firefox, Opera, Safari
© 2008 Jeremy McPeak