The following PureScript code:
fibs 0 = 1
fibs 1 = 1
fibs n = fibs (n-1) + fibs (n-2)
Compiles to the following JavaScript:
var fibs = function (v) {
if (v === 0) {
return 1;
};
if (v === 1) {
return 1;
};
return fibs(v - 1 | 0) + fibs(v - 2 | 0) | 0;
};
Which all makes perfect sense, except the "| 0"s seem a bit unnecessary. Is it an optimisation? Or to make it robust to undefined or NaNs ?
Inserting |0
for integers ensures that values do not accidentally become floating-point or fall out of range for int32 - this guarantees that bitwise operations will behave as expected on Int
values too.
In theory it could be an optimisation, as asm.js sees |0
as a hint that a value is an int too, but I think in practice that's wishful thinking that it makes a difference in the average JS program!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments