Hacker News new | ask | show | jobs
by phoboslab 5127 days ago
> (...) even the optimized JavaScript libraries like Box2D becomes faster when ported to JSX.

Box2D is a C++ library that has been ported to ActionScript and then, from ActionScript, converted to JavaScript - not by hand, but by a bunch of scripts[1]. The JS version of Box2D still carries around a lot of unnecessary weight from the original C++ and ActionScript versions and has much room for improvement.

A rewrite or "smart" conversion from C++ to JS, that endorses JS instead of trying to emulate C++ or ActionScript, should be able to improve performance a lot.

I don't know how Box2D was ported to JSX. Maybe it was a rewrite by a human? In any case, I'm not saying what they do isn't impressive, but calling Box2D an "optimized JavaScript library" is just plain wrong.

[1] There are several different JS ports of Box2D available, but, to my knowledge, none of which is a sensible rewrite by a human.

2 comments

You're definitely right about that.

Probably the biggest inefficiency with all the ports is that they don't pool vectors. In the C++ version, vectors are often on the stack, which is way more efficient. In JS, it creates large numbers of temporary vectors that must be garbage collected.

For my iPad port of one of my Flash games (written in Haxe, so it was actually being translated back into C++), I spent quite a few days profiling the places that were vectors were being created the most, and modifying them to use a pool.

Then some verification code in debug mode checked that there was never a dead vector being written to or read from.

In pathological places with tons of collisions, that got the speed from 2 fps to 30.

As a former contributor to Box2DX, I'm impressed with your knowledge of the state of Box2D ports, did you contribute to one of them?