To answer both questions:
Can any of you understand why we are seeing this problem?
JavaScript numbers are standard IEEE doublings.
The largest integer value that JavaScript may contain is: 9007199254740992
However, the bit-shift operator works on 32-bit ints.
And I quote the language specification :
5. Let lnum be ToInt32 (lval).
...
Returns the result of performing a right shift of the lnum shift sign with the shiftCount bits. The most significant bit multiplies. The result is a signed 32-bit integer.
Do you see any flaws using "multiplication" instead of "bit-shift"?
It still looks slower, but you should really avoid working with very large numbers in JavaScript anyway.
There are libraries that handle very large numbers, and there are functions that are discussed to add better support to large numbers in the language (ES7 value types).
source share