Skip to content

Instantly share code, notes, and snippets.

@timokoessler
Created July 1, 2023 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timokoessler/0d5075aa936d1d1e92e931051f970add to your computer and use it in GitHub Desktop.
Save timokoessler/0d5075aa936d1d1e92e931051f970add to your computer and use it in GitHub Desktop.
Performance of cidr-matcher vs. netparser vs. array
const CIDRMatcher = require('cidr-matcher');
const netparser = require("netparser");
function generateRandomIP() {
let ip = [];
for (let i = 0; i < 4; i++) {
ip.push(Math.floor(Math.random() * 255));
}
return ip.join(".");
}
// Create a array with 10000 random ip addresses
const ipAddresses = [];
for (let i = 0; i < 100000; i++) {
ipAddresses.push(generateRandomIP() + '/32');
}
const matcher = new CIDRMatcher(ipAddresses);
const matcher2 = new netparser.Matcher(ipAddresses);
let matches = 0;
// Test the performance of the cidr-matcher
console.time('cidr-matcher');
for (let i = 0; i < 10000; i++) {
if(matcher.contains(generateRandomIP())){
matches++;
}
}
console.timeEnd('cidr-matcher');
// Test the performance of the netparser
console.time('netparser');
for (let i = 0; i < 10000; i++) {
if(matcher2.get(generateRandomIP())){
matches++;
}
}
console.timeEnd('netparser');
// Test the performance of a array
console.time('array');
for (let i = 0; i < 10000; i++) {
if(ipAddresses.includes(generateRandomIP())){
matches++;
}
}
console.timeEnd('array');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment