- Notifications
You must be signed in to change notification settings - Fork1
cidr-tools but up to 20x faster
License
NotificationsYou must be signed in to change notification settings
SukkaW/fast-cidr-tools
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Tools to work with IPv4 and IPv6 CIDR, but 20x faster thancidr-tools, and isnot Pure ESM (publish both CommonJS and ESM).
RequiresBigInt supports.
import{ip2bigint,// support both IPv4 and IPv6bigint2ip,// support both IPv4 and IPv6contains,exclude,expand,merge,overlap}from'fast-cidr-tools';contains(['1.0.0.0/24','2.0.0.0/24'],['1.0.0.1'])//=> trueexclude(['::1/127'],['::1/128'])//=> ['::/128']expand(['2001:db8::/126'])//=> ['2001:db8::', '2001:db8::1', '2001:db8::2', '2001:db8::3']merge(['1.0.0.0/24'],['1.0.1.0/24']);//=> ['1.0.0.0/23']overlap(['1.0.0.0/24'],['1.0.0.128/25'])//=> true
fast-cidr-tools is very fast, and is specfically optimized for IPv4. In some cases, it can be 20x faster thancidr-tools.fast-cidr-tools acheive this performance by:
- Doing less things, E.g.:
cidr-toolswill cast input to an array if it is string (cidrTools.merge('1.0.0.0/24', '1.0.1.0/24')).fast-cidr-toolsonly supports array input.cidr-toolssort the return value ofmergeandexcludeby default.fast-cidr-tools's sort is opt-in.- And many more.
- Use efficient calculation and avoid unnecessary string operations.
- how
cidr-toolsandfast-cidr-toolscalculate the "biggest power of two":// cidr-toolsfunctionbiggestPowerOfTwo(num){if(num===0n)return0n;return2n**BigInt(String(num.toString(2).length-1));}// fast-cidr-tools// Not the actual implementation: fast-cidr-tools actually inlines the calculation// for better performanceconstuint64=newBigUint64Array(1);constuint32=newUint32Array(uint64.buffer);functionclz64(bigint:bigint){uint64[0]=bigint;letr=Math.clz32(uint32[1]);if(r===32){r+=Math.clz32(uint32[0]);}returnr;}functionbiggestPowerOfTwo(num:bigint){if(num===0n)return0n;constpower=BigInt(64-clz64(size)-1);return2n**(power===-1n ?128n :power);}
- how
cidr-toolsandfast-cidr-toolscalculate the prefix of a CIDR:// cidr-toolsconstzeroes=(part.end+1n-part.start).toString(2);constprefix=bits[v]-(zeroes.match(/0/g)||[]).length;// fast-cidr-toolsfunctionfast_popcnt(value:bigint|number){letv=Number(value);v-=v>>>1&0x55_55_55_55;v=(v&0x33_33_33_33)+(v>>>2&0x33_33_33_33);returnBigInt((((v+(v>>>4))&0x0F_0F_0F_0F)*0x01_01_01_01)>>>24);}constuint64_0=newBigInt64Array(1);constuint32_0=newInt32Array(uint64_0.buffer);functionfast_popcnt64(value:bigint){uint64_0[0]=value;returnfast_popcnt(uint32_0[0])+fast_popcnt(uint32_0[1]);}constprefix=bits[v]-(v===4 ?fast_popcnt(end-start) :fast_popcnt64(end-start));
- how
- Use a more compact internal data structure. Compare how
cidr-toolsandfast-cidr-toolsrepresent a CIDR internally:// cidr-toolsparse('::/64');//=> { cidr: '::/64', version: 6, prefix: '64', start: 0n, end: 18446744073709551615n }// fast-cidr-toolsparse('::/64');//=> [0n, 18446744073709551615n, 6]
- Avoid repeated calculations. Notice how
fast-cidr-toolspre-parse inputs instead of parsing them repeatedly:
fast-cidr-tools ©Sukka, Authored and maintained by Sukka with help from contributors (list).
Personal Website ·Blog · GitHub@SukkaW · Telegram Channel@SukkaChannel · Twitter@isukkaw · Keybase@sukka
About
cidr-tools but up to 20x faster
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.