Performance of bitwise operators in javascript

后端 未结 12 2378
难免孤独
难免孤独 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:29

    Using JavaScript in its Windows Scripting Host JScript incarnation, you might have cause to use bitwise operators to pick out flags in values returned from WMI or Active Directory calls. For example, the User Access value of a user's record in AD contains several flags packed into one long integer.

    ADS_UF_ACCOUNTDISABLE = 0x00000002;
    
    if (uac & ADS_UF_ACCOUNTDISABLE == ADS_UF_ACCOUNTDISABLE) {
      // user account has been disabled
    }
    

    Or someone's arbitrary table structure may contain such a field, accessible through ADO with JScript.

    Or you may want to convert some retrieved data into a binary representation on any platform, just because:

    BinaryData = "L";
    BinaryString = BinToStr(BinaryData, ".", "x");
    
    // BinaryString => '.x..xx..'
    

    So there are numerous reasons why one might want to do bit manipulation in JavaScript. As for performance, the only way to know is to write it and test it. I suspect in most cases it would be perfectly acceptable, not significantly worse than any other of the multitude of inefficiencies these systems contain.

提交回复
热议问题