Skip to content

Instantly share code, notes, and snippets.

@RafalWilinski
Created May 29, 2019 17:14
Show Gist options
  • Save RafalWilinski/c41ee9370f8447b95d485f98042dcb29 to your computer and use it in GitHub Desktop.
Save RafalWilinski/c41ee9370f8447b95d485f98042dcb29 to your computer and use it in GitHub Desktop.
let kv = {};
let set = new Set();
let map = new Map();
const { performance } = require('perf_hooks');
const numOfBlacklisted = 3500000;
const numOfChecks = 100000;
const putToKv = (ip) => {
kv[ip] = true;
}
const putToSet = (ip) => {
set.add(ip);
}
const putToMap = (ip) => {
map.set(ip, true);
}
const checkSet = (ip) => set.has(ip);
const checkMap = (ip) => map.has(ip);
const checkKV = (ip) => !!kv[ip];
// Functionality tests
putToMap('1.1.1.1');
console.log(checkMap('1.1.1.1')); // true
console.log(checkMap('1.1.1.2')); // false
putToKv('1.1.1.1');
console.log(checkKV('1.1.1.1')); // true
console.log(checkKV('1.1.1.2')); // false
putToSet('1.1.1.1');
console.log(checkSet('1.1.1.1')); // true
console.log(checkSet('1.1.1.2')); // false
const createIP = () => {
const ipArr = [];
for (let i = 0; i < 4; i++) {
ipArr.push(Math.floor(Math.random() * 100000 % 256));
}
return ipArr.join('.');
}
let start = performance.now();
for (let i = 0; i < numOfBlacklisted; i++) {
putToKv(createIP());
}
let end = performance.now();
console.log('KV PUT', end - start);
start = performance.now();
for (let i = 0; i < numOfChecks; i++) {
checkKV(createIP());
}
end = performance.now();
console.log('KV Check', end - start);
kv = {}; // free up memory
start = performance.now();
for (let i = 0; i < numOfBlacklisted; i++) {
putToSet(createIP());
}
end = performance.now();
console.log('--------------------------------------');
console.log('Set PUT', end - start);
start = performance.now();
for (let i = 0; i < numOfChecks; i++) {
checkSet(createIP());
}
end = performance.now();
console.log('Set Check', end - start);
set = undefined; // free up memory
start = performance.now();
for (let i = 0; i < numOfBlacklisted; i++) {
putToMap(createIP());
}
end = performance.now();
console.log('--------------------------------------');
console.log('Map PUT', end - start);
start = performance.now();
for (let i = 0; i < numOfChecks; i++) {
checkMap(createIP());
}
end = performance.now();
console.log('Map Check', end - start);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment