I\'ve seen some performance critical javascript code, like the one on this project that makes extensive use of bitwise OR operations with 0. Ex:
GameBoyAdvan
Referencing the Ecmascript 5 spec: 11.10 Binary Bitwise Operators, namely
The production
A : A @ B, where@is one of the bitwise operators in the productions above (&;^;|), is evaluated as follows:Let
lrefbe the result of evaluating A.
LetlvalbeGetValue(lref).
Letrrefbe the result of evaluating B.
LetrvalbeGetValue(rref).
LetlnumbeToInt32(lval).
LetrnumbeToInt32(rval).
Return the result of applying the bitwise operator@ tolnumandrnum. The result is a signed 32 bit integer.
And noting that ToInt32() is defined as
Let
numberbe the result of callingToNumberon the input argument.
If number isNaN,+0,−0,+∞, or−∞, return+0.
LetposIntbesign(number) * floor(abs(number)).
Letint32bitbeposIntmodulo2^32; that is, a finite integer valuekofNumbertype with positive sign and less than2^32in magnitude such that the mathematical difference ofposIntandkis mathematically an integer multiple of2^32.
Ifint32bitis greater than or equal to2^31, returnint32bit − 2^32, otherwise returnint32bit.
It then logically follows (which you can confirm in your own console) that for example
((Math.pow(2, 32)) + 2) | 0 === 2
(Math.pow(2, 31)) | 0 === -2147483648 === -(Math.pow(2, 31))
And so forth.
Shortly put, the operation turns the number to a 32-bit integer (which has its knacks, see the second example above and the ToInt32() definition for an explanation) and then does a logical or with zero which doesn't change the output beyond the first conversion.
Essentially it's a very cost-efficient way to turn a number into a 32-bit integer because 1) it relies on browser's built-in ToInt32(); and 2) ToInt32(0) short-circuits to 0 (see the spec above) and therefore adds practically no additional overhead.