2

Possible Duplicate:
Where would I use a bitwise operator in JavaScript?

In c/c++ bitwise operations are faster than normal(arithmetic) operations(significant atleast in low performance processors). Does the same apply in js? I don't think as the reason its faster in c is bitwise operations are hardwired and usually are completed in 1 processor cycle. But js runs within browser which doesn't have any such hardware(registers I mean) access. I am not sure (around 70% sure :) ). What are typical(or some smarter) uses of bitwise operators (especially in js but I would like to know others too). Please correct me if I am wrong anywhere.

Community
  • 1
  • 1
0xc0de
  • 8,028
  • 5
  • 49
  • 75

2 Answers2

5

Some bitwise operators are faster than arithmetic operators in some cases. It's hard to optimise Javascript, because the performance varies greatly betwen browsers, platforms and computer models.

Modern browsers compile the Javascript code into native code, so some things that are said about compiled languages are also relevant for Javascript. However, some things that are said about compiled languages are getting more and more inaccurate with newer processors. It's for example not relevant to look at the performance of a single processor operation any more, as operations are run in parallel. You don't look at how many cycles an operation takes any more, but how many operations you can do in a cycle.

To determine if a specific operation is faster or slower than another in Javascript, you would have to test it on a wide variety of computers and browsers, and look at the average, best case and worst case. Even then, any specific result that you get, woudl get more and more out of date for each new browser version that is released.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
3

Bitwise operators in JS are slow. Really slow compared to C. The reason is that in JS, all numbers are represented as double-precision floating point numbers, so to perform a bitwise operation, the runtime has to convert them to 32-bit integers and back.

That's not to say they aren't useful. e.g., Node#compareDocumentPosition returns a bitmask, and something.length >>> 0 is a common way of getting the length property of something or zero if length isn't a number or is NaN. Also, a / b | 0 is a fast way to do Math.floor(a / b), assuming a and b are >= 0.

Peter C
  • 6,219
  • 1
  • 25
  • 37
  • Won't `something.length || 0` work too? – Matthew Mar 13 '12 at 15:28
  • In the case of `undefined`, `null`, and `NaN` yes, but `>>> 0` also works in the case of anything that's not a number. Good point, I'll edit that. – Peter C Mar 13 '12 at 22:35
  • [Don't use bitwise operations as a substitute for `Math.floor`. It's unreadable, and there is no performance gain in modern JS.](http://stackoverflow.com/q/10890486/201952) – josh3736 Jun 12 '12 at 16:20
  • @josh3736 It's *less* readable, not *unreadable*. If you've seen it before and know what it means, it's not bad at all. [And sadly, it really is still faster.](http://jsperf.com/js-floors/2) – Peter C Jun 12 '12 at 20:57
  • The JavaScript *semantics* guarantee the range of a number and the operation of the bitwise operators. However optimized/JIT JS implementations will use int32 type inference and type tagging (to avoid double-boxing) to result in *significantly faster* execution. In this context (in 2014) the first paragraph is unjustified. – user2864740 Oct 09 '14 at 23:29