[1] https://james.darpinian.com/blog/integer-math-in-javascript
[1] https://james.darpinian.com/blog/integer-math-in-javascript
A wasm b-tree, skip list, rope, etc seems to outperform the equivalent javascript code by many times.
Edit: I just whipped up a real benchmark to test this, replaying some text editing traces[1].
Replaying the automerge-perf editing trace in javascript naively takes 610ms. Using a javascript based skip list it takes 77ms[2]. In native rust, with a rust port of the same skip list code I can process the same editing trace in 6ms[3]. Or 20ms when that rust code is compiled to wasm.
So in this case, we're seeing about a 4x performance improvement using wasm, or 13x performance improvement using native code.
[1] https://github.com/josephg/crdt-benchmarks
[2] This code https://gist.github.com/josephg/bcb2e74e52dc9c4651249fdffc48... using this library: https://www.npmjs.com/package/jumprope
The most performance-critical section is the "arbiter applyImpulse" physics code[2], which is all math.
I tried putting everything in as big TypedArray to avoid all the object lookups, but performance got slightly worse, not better. I still have no idea why. I think the reason is that the optimizer couldn't optimize the typedarray lookups at the time. Not in the same way it can optimize math on normal objects.
Maybe v8 is more clever now? I dunno!
The biggest performance win I got was from inlining vector fields. Chipmunk heavily uses {x,y} 2d vectors. Lots of objects looked like this: {pos: {x, y}, velocity: {x, y}, ...}. Flattening everything to this {posx, posy, velocityx, velocityy, ...} made the code uglier. But it caused a massive performance improvement because it reduced the number of objects allocated on the heap. (This one change reduced the number of heap allocations by about 70% from memory, and it caused a double-digit performance improvement overall.)
As I see it, Javascript will always be much slower than other languages for complex data structures because everything is stored on the heap.
[1] https://github.com/josephg/chipmunk-js
[2] https://github.com/josephg/Chipmunk-js/blob/ec447072458f4009...
I suspects if I re-ran that test, performance would have improved across the board but I'd get the same relative performance result with TypedArray code.
It would be fascinating to try though. I wish I kept the code.
> [1] Add | 0 after every math operation to get a 32-bit signed integer result, and >>> 0 for a 32-bit unsigned integer result.
That's definitely gonna cause a few "wtf?" moments for everyone else on your team, lol
Source? Every benchmark I've seen that has JS comparable / faster than C uses an extremely suboptimal C implementation.