Skip to content

Commit baf5b0d

Browse files
committed
os: improve network interface performance
This reduces the overhead of getCIDR() to a minimum. No array is allocated anymore and parts are directly sliced out of the netmask string instead. Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 7d68b7b commit baf5b0d

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

lib/os.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const {
2828
ObjectDefineProperties,
2929
StringPrototypeEndsWith,
3030
StringPrototypeSlice,
31-
StringPrototypeSplit,
3231
SymbolToPrimitive,
3332
} = primordials;
3433

@@ -225,28 +224,39 @@ function getCIDR(address, netmask, family) {
225224
let range = 10;
226225
let groupLength = 8;
227226
let hasZeros = false;
227+
let lastPos = 0;
228228

229229
if (family === 'IPv6') {
230230
split = ':';
231231
range = 16;
232232
groupLength = 16;
233233
}
234234

235-
const parts = StringPrototypeSplit(netmask, split);
236-
for (let i = 0; i < parts.length; i++) {
237-
if (parts[i] !== '') {
238-
const binary = NumberParseInt(parts[i], range);
239-
const tmp = countBinaryOnes(binary);
240-
ones += tmp;
235+
for (let i = 0; i < netmask.length; i++) {
236+
if (netmask[i] !== split) {
237+
if (i + 1 < netmask.length) {
238+
continue;
239+
} else {
240+
i++;
241+
}
242+
}
243+
const part = netmask.slice(lastPos, i);
244+
lastPos = i + 1;
245+
if (part !== '') {
241246
if (hasZeros) {
242-
if (tmp !== 0) {
247+
if (part !== '0') {
243248
return null;
244249
}
245-
} else if (tmp !== groupLength) {
246-
if ((binary & 1) !== 0) {
247-
return null;
250+
} else {
251+
const binary = NumberParseInt(part, range);
252+
const binaryOnes = countBinaryOnes(binary);
253+
ones += binaryOnes;
254+
if (binaryOnes !== groupLength) {
255+
if ((binary & 1) !== 0) {
256+
return null;
257+
}
258+
hasZeros = true;
248259
}
249-
hasZeros = true;
250260
}
251261
}
252262
}

0 commit comments

Comments
 (0)