Created
July 1, 2023 13:57
-
-
Save timokoessler/0d5075aa936d1d1e92e931051f970add to your computer and use it in GitHub Desktop.
Performance of cidr-matcher vs. netparser vs. array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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