Stewart Platt's Blog - Optimising JavaScript for v8

Optimising JavaScript for v8

I hope I'll do a couple of these posts as I learn more, because writing fast code in C/C++ appears to have almost nothing to do with writing fast code in JavaScript.

Writing a BitStream in pure JS I tried the following operation:

// Calculate the number of bits to write in this 
var bitsToWrite = bitsLeftInByte < bitLength ? bitsLeftInByte : bitLength;

On my first round of optimisation, this seemed like a possible candidate.

Math.min(bitsLeftInByte, bitLength);

Nope, 10% slower.

var xmy = bitsLeftInByte - bitLength;
var bitsToWrite = bitLength + (xmy & (xmy >> 31));

Aha, bit twiddling hacks! About 5% slower.

I think I need to see some assembly to make any sense of this.
I'd like to say I will do some command-line fu, but more likely is I will attach OllyDbg in a Windows build of NodeJS.