Skip to content

Instantly share code, notes, and snippets.

@coderholic
Created October 7, 2020 03:23
Show Gist options
  • Save coderholic/a85425346a566418a35c0718580b3be8 to your computer and use it in GitHub Desktop.
Save coderholic/a85425346a566418a35c0718580b3be8 to your computer and use it in GitHub Desktop.
const mmap = require('mmap-io')
const { noop } = require('lodash')
const fs = require('fs')
const maxmind = require('maxmind')
const fake_cache = {
get: noop,
set: noop
}
const loader = (path, use_mmap) => {
if (use_mmap) {
const { size } = fs.statSync(path)
const fd = fs.openSync(path, 'r')
const flags = mmap.MAP_SHARED | mmap.MAP_POPULATE
const buffer = mmap.map(
size,
mmap.PROT_READ,
flags,
fd,
0,
mmap.MADV_WILLNEED
)
fs.closeSync(fd)
return buffer
} else {
const buffer = fs.readFileSync(fullPath)
return buffer
}
}
mmap_file = new maxmind.Reader(loader('./files/registry.mmdb', true))
no_mmap_file = new maxmind.Reader(loader('./files/registry.mmdb', true))
const do_test = (lib) => {
console.time('Test');
for (let i = 0; i < 100000; i++) {
lib.get('8.8.8.8')
lib.get('8.8.4.4')
lib.get('1.1.1.1')
}
console.timeEnd('Test');
}
console.log("MMAP file with cache")
do_test(mmap_file)
console.log("Non-MMAP file with cache")
do_test(no_mmap_file)
mmap_file.decoder.cache = fake_cache
no_mmap_file.decoder.cache = fake_cache
console.log("MMAP file NO cache")
do_test(mmap_file)
console.log("Non-MMAP file NO cache")
do_test(no_mmap_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment