Skip to content

Commit 594bb9d

Browse files
BridgeARdanielleadams
authored andcommitted
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> PR-URL: #46598 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 4cf3de8 commit 594bb9d

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

‎lib/os.js‎

Lines changed: 21 additions & 12 deletions
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,38 @@ function getCIDR(address, netmask, family) {
225224
letrange=10;
226225
letgroupLength=8;
227226
lethasZeros=false;
227+
letlastPos=0;
228228

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

235-
constparts=StringPrototypeSplit(netmask,split);
236-
for(leti=0;i<parts.length;i++){
237-
if(parts[i]!==''){
238-
constbinary=NumberParseInt(parts[i],range);
239-
consttmp=countBinaryOnes(binary);
240-
ones+=tmp;
235+
for(leti=0;i<netmask.length;i++){
236+
if(netmask[i]!==split){
237+
if(i+1<netmask.length){
238+
continue;
239+
}
240+
i++;
241+
}
242+
constpart=StringPrototypeSlice(netmask,lastPos,i);
243+
lastPos=i+1;
244+
if(part!==''){
241245
if(hasZeros){
242-
if(tmp!==0){
246+
if(part!=='0'){
243247
returnnull;
244248
}
245-
}elseif(tmp!==groupLength){
246-
if((binary&1)!==0){
247-
returnnull;
249+
}else{
250+
constbinary=NumberParseInt(part,range);
251+
constbinaryOnes=countBinaryOnes(binary);
252+
ones+=binaryOnes;
253+
if(binaryOnes!==groupLength){
254+
if((binary&1)!==0){
255+
returnnull;
256+
}
257+
hasZeros=true;
248258
}
249-
hasZeros=true;
250259
}
251260
}
252261
}

0 commit comments

Comments
 (0)