Skip to content

Commit 1c6e014

Browse files
bnoordhuisFishrock123
authored andcommitted
stream: micro-optimize high water mark calculation
Don't iterate over all 32 bits, use some hacker's delight bit twiddling to compute the next power of two. The logic can be reduced to `n = 1 << 32 - Math.clz32(n)` but then it can't easily be backported to v2.x; Math.clz32() was added in V8 4.3. PR-URL: #2479 Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent f1f4b4c commit 1c6e014

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

lib/_stream_readable.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ function roundUpToNextPowerOf2(n) {
198198
} else {
199199
// Get the next highest power of 2
200200
n--;
201-
for (var p = 1; p < 32; p <<= 1) n |= n >> p;
201+
n |= n >>> 1;
202+
n |= n >>> 2;
203+
n |= n >>> 4;
204+
n |= n >>> 8;
205+
n |= n >>> 16;
202206
n++;
203207
}
204208
return n;

0 commit comments

Comments
 (0)