Performance of bitwise operators in javascript

后端 未结 12 2346
难免孤独
难免孤独 2020-12-14 08:33

One of the main ideas behind using bitwise operators in languages like C++/java/C# is that they\'re extremely fast. But I\'ve heard that in javascript they\'re very slow (ad

相关标签:
12条回答
  • 2020-12-14 09:13

    There is an NES emulator written in JavaScript - it seems to make plenty of use of bitwise operations.

    0 讨论(0)
  • 2020-12-14 09:15

    This is quite an old question, but no one seemed to answer the updated version.

    The performance hit that you get with JavaScript that doesn't exist in C/C++ is the cast from floating point representation (how JavaScript strores all of its numbers) to a 32 bit integer to perform the bit manipulation and back.

    0 讨论(0)
  • 2020-12-14 09:16

    Nobody uses hex anymore?

    function hextoRgb(c) {
        c = '0x' + c.substring(1);
        return [(c >> 16) & 255, (c >> 8) & 255, c & 255]; 
    }
    
    var c1 = hextoRgb('#191970');
    alert('rgb(' + c1.join(',') + ')');
    
    0 讨论(0)
  • 2020-12-14 09:16

    I use bitwise shift of zero in JS to perform quick integer truncation:

    var i=3.141532;
    var iTrunc=i>>0; //3
    
    0 讨论(0)
  • 2020-12-14 09:19

    I found some good info @ http://dreaminginjavascript.wordpress.com/2009/02/09/bitwise-byte-foolish/

    Apparently they perform very well these days. Why would you use them? Same reason you would anywhere else.

    0 讨论(0)
  • 2020-12-14 09:19

    When speed is paramount, you can use them for bit-masking: http://snook.ca/archives/javascript/storing_values/

    Also, if you need to support Netscape 4, you'd use them to deal with Document.captureEvents(). Not that any respectable company would have you write JS for NS4...

    0 讨论(0)
提交回复
热议问题