GUIDES Optimizing Embedded JavaScript Operating on Bits
About
Binary Data
Index
Convert String to ArrayBuffer
Convert ArrayBuffer to String
Convert ArrayBuffers to String
Handle Errors Converting ArrayBuffer to String
Immutable ArrayBuffers
Resize an ArrayBuffer
Combine ArrayBuffers
Convert Base64 to Binary Data
Convert Binary Data to Base64
Convert Binary Data to Hex
Convert Hex to Binary Data
Calculate CRC for Binary Data
Compress Binary Data – One Buffer
Compress Binary Data – Streaming
Decompress Binary Data – One Buffer
Decompress Binary Data – Streaming
Callbacks
Index
One-Time Callback
Repeating Callback
Repeating Callback with Initial Delay
Immediate Callback
Reschedule Callback
Cancel Callback
Suspend Callback
What About setTimeout?
Optimizing Embedded JavaScript
Index
When to Optimize
Know Where to Optimize
Looping through an Array
Iterating Over a String
Building a String
Avoid Copying Buffers
Accessing Properties
Map versus Object
Appending to an Array
Operating on Bits
Defining Class Methods
Reducing Stack Use
Time
Index
Get Unix Time
Get Time of Day
Get Date
Get Time Since System Start
Get Microseconds
Set System Date and Time
Set Real-Time Clock Time
Get Time and Date from Network
Get Time and Date from Real-Time Clock
Sleep

Operating on Bits

Embedded JavaScript developers often need to operate on the bits of an integer value. JavaScript supports this, even though it doesn't have an integer data type. The XS JavaScript engine is optimized for integer operations. With a little care, you can get the fastest possible bit operations and avoid some subtle bugs.

When OR-ing values together always use the OR operator | and never +. The OR operator is guaranteed to perform an integer operation while the addition operator can operate on many kinds of values including strings, floating point values, and BigInt.

/* BEFORE */
let flags = Options.cache + Options.wrap;

let flags = Options.cache;
flags += Options.wrap;

/* AFTER */
let flags = Options.cache | Options.wrap;

let flags = Options.cache;
flags |= Options.wrap;

When shifting values, use the shift operators instead of multiplying or dividing by powers of two. The shift operators ensure the values are 32-bit unsigned integers, which is what you want for bit operations. Multiplying and dividing are slower and may give unexpected results.

let value = 0x8000_0000;
let shifted = value * 2;
// => shifted is 8589934592

let value = 0x8000_0000;
let shifted = value << 1;
// => shifted is 0

let value = 1;
value /= 2;
// => value is 0.5

let value = 1;
value >>= 1;
// => value is 0