1

According to w3schools

Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.

JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.

So my question comes: if we want to perform a bitwise operation, how javascript translates that 64-bit IEEE 754 standard float to an ordinary 32-bit integer, and how efficient it is? From intuition, converting numbers will be costly, so will it still be more efficient to use bit shifting than multiply by 2n?

Thank you very much!

3
  • 2
    Each implementation can be different, so it might not be possible to say what the most efficient method would be. Also, if you plan on doing this a lot, you actually can use a typed array. W3Schools isn't entirely correct... there are typed arrays that might be more efficient for bitwise, but you would have to test to be sure. developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures Commented Feb 1, 2015 at 5:26
  • 1
    If you want to "know" how efficient one operation is vs. another, then you MUST test and measure the performance and you must characterize it in whatever browsers and browser versions you consider relevant. jsperf.com is a good tool for running such tests. Each browser has its own implementation so there is no theoretical answer. Measure, measure, measure. Commented Feb 1, 2015 at 5:29
  • Did you find that simple multiplication was performing poorly? The most readable and thus maintainable code would be code that doesn't use bit shifting for multiplication. Commented Feb 1, 2015 at 6:40

1 Answer 1

4

From The Good Parts:

Bitwise operators work with integers. JavaScript doesn't have integers. It only has double precision floating-point numbers. So, the bitwise operators convert their number operands into integers, do their business, and then convert them back. In most languages, these operators are very close to the hardware and very fast. In JavaScript, they are very far from the hardware and very slow.

Ultimately, though, the behavior will nearly always be implementation-specific. Some JavaScript environments may - today or in the future - be able to reason that this-or-that piece of code will always be handled like an int, or perhaps store vars as ints by default.

The only way to know for sure is to profile your code. A tool like JSPerf might be helpful. It might also give you contradictory metrics - what runs faster in one browser might perform poorly in another JavaScript engine. Performance gains might also depend on heuristics that your own production code unknowingly breaks.

The real question, though, is a different one: what are you planning to do that makes multiplication performance a critical factor in performance? Because it's probably not something you should be doing in a UI-blocking client side script.

Sign up to request clarification or add additional context in comments.

2 Comments

ic, that clarifies my doubts. Thx :D
One reason to use bitwise operators would be to implement bitboards for Chess, Connect Four, etc. Hence, there are indeed legitimate use cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.