Unlock JavaScript Performance: When and How to Use Bitwise Operators Effectively
This article explains why JavaScript developers often avoid bitwise operators, highlights the specific scenarios where they provide performance and code‑size benefits—such as fast integer conversion with x|0, using ~ for -1 checks, creating bit fields, and leveraging them in databases and BigInt.
Why Bitwise Operators Are Often Avoided
Many JavaScript developers avoid bitwise operators because they can be confusing with logical operators and because JavaScript numbers are 64‑bit while bitwise operations always return 32‑bit integers, leading to unexpected results.
When Bitwise Operators Shine
They can provide performance gains and more concise code in specific scenarios, such as fast integer conversion using x|0 (or ~~x) which never yields NaN and is faster than parseInt() or Math.trunc().
"3.0" | 0 // => 3
{} | 0 // => 0
parseInt({}, 10) // => NaN
Math.trunc({}) // => NaNHowever, x|0 is unsafe for numbers larger than 32 bits.
"2147483649" | 0 // => -2147483647
parseInt("2147483648", 10) // => 2147483648ESLint’s no-bitwise rule offers an int32Hint option to support x|0 when needed.
Using ~ to Test for –1
Instead of checking index === -1, the bitwise NOT operator ~ provides a shorter, clearer test.
!!~1 // => true
!!~0 // => true
!!~-1 // => false
!!~-2 // => trueBit Fields in JavaScript
By treating a 32‑bit integer as a collection of boolean flags, you can store multiple on/off settings in a single number.
class Bits {
static get(field, index) {
return (field >> index) & 1;
}
static set(field, index, value = 1) {
return (field & ~(1 << index)) | (value << index);
}
}Example: packing notification preferences into a bit field.
let field = parseInt('011', 2); // => 3
Bits.get(field, 0) // => 1 (new comment)
Bits.get(field, 1) // => 1 (new article)
Bits.get(field, 2) // => 0 (user follow)
field = Bits.set(field, 0, 0); // turn off comment notificationBitwise Operators in Databases
Databases such as PostgreSQL and MongoDB support bitwise queries. In MongoDB you can use $bitsAllSet to find documents where specific bits are set and $bit to update bit fields.
// Find users with new‑article notification (bit 1) set
db.collection('users').find({ field: { $bitsAllSet: [1] } });
// Enable user‑follow notification (bit 2) for user id 10
db.collection('users').update(
{ id: 10 },
{ $bit: { field: { or: (1 << 2) } } }
);Future with BigInt
BigInt removes the 32‑bit limitation, allowing bitwise operations on arbitrarily large integers and making it possible to use a single BigInt as an unlimited bit field. The proposal is at stage 3 and already available in V8‑based runtimes such as Chrome and Node.js.
Node Underground
No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
