I was talking to Ryan Westphal the other day and he was saying how he’d been playing round with ASM.js and you could hand code it fairly easily and it was FAST. I’ve been playing around with a crazy fractal generating map so I figured that would be as good a place as any to try out ASM.js.

The code for the different fractal types was already stored in an object so switching it to be an asmjs style module wasn’t too bad, code before:

code after:

I was able to smooth out the bugs as firefox versions that support it will give you the line numbers that prevent your asm code from compiling, after I finally got rid of the last of the errors primarily static type N00B mistakes like pow(y,3) but also asmjs ones like trying to initialize a variable to undefined or to an expression and ones related to not reading the allowed operators closely (no logical and). So I fired it up and nothing happend.

Then I realized that ASM seems to be turned off if you open up the debug console. So close tools and hard refresh and … nothing, it’s actually slower in chrome too.

After some intense microbenchmarks and help from John-David Dalton and Vyacheslav Egorov on twitter I was able to figure out that

Replacing the logical and with a binary and (&&->&) was what was killing chrome perf

Despite what Firefox tells you DO NOT USE THE FUNCTION CONSTRUCTOR

ASM doesn't optimize all code perf.

I have a feeling that there is an issue with the construction of my loop that's preventing it from optimizing. More details to come, oh and the current fractal demo did I mention that all of this is inside a web worker?

Update: I was planing more updates sooner but then Ubuntu started updating to Raring and my laptop started overheating when I did anything

Update 2: I created a perf with the diag example from the spec and found that adding the words “use asm” cause it to go approximately 1/3 the speed. But for some reason when firefox nightly tries to post the results it gets a 413 so hears a screen shot.

Update 3: This is the perf that Ryan was talking about when he said asm was fast. This is the non-recursive version that I made, the asm version is now 206,350x faster, but the non-asm one is 1,654,225x faster giving it a commanding lead. I will admit I am not a C coder, I can make some pretty optimized JavaScript but this lower level stuff I may be way off on, feel free to give me a shout on twitter with any suggestions.