Skip to content

Commit 2a41858

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 2a41858

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

lib/os.js

+22-11
Original file line numberDiff line numberDiff line change
@@ -225,28 +225,39 @@ function getCIDR(address, netmask, family) {
225225
let range = 10;
226226
let groupLength = 8;
227227
let hasZeros = false;
228+
let lastPos = 0
228229

229230
if (family === 'IPv6') {
230231
split = ':';
231232
range = 16;
232233
groupLength = 16;
233234
}
234235

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;
236+
for (let i = 0; i < netmask.length; i++) {
237+
if (netmask[i] !== split) {
238+
if (i + 1 < netmask.length) {
239+
continue;
240+
} else {
241+
i++;
242+
}
243+
}
244+
const part = netmask.slice(lastPos, i);
245+
lastPos = i + 1;
246+
if (part !== '') {
241247
if (hasZeros) {
242-
if (tmp !== 0) {
248+
if (part !== '0') {
243249
return null;
244250
}
245-
} else if (tmp !== groupLength) {
246-
if ((binary & 1) !== 0) {
247-
return null;
251+
} else {
252+
const binary = Number.parseInt(part, range);
253+
const binaryOnes = countBinaryOnes(binary);
254+
ones += binaryOnes;
255+
if (binaryOnes !== groupLength) {
256+
if ((binary & 1) !== 0) {
257+
return null;
258+
}
259+
hasZeros = true;
248260
}
249-
hasZeros = true;
250261
}
251262
}
252263
}

0 commit comments

Comments
 (0)