Skip to content

Instantly share code, notes, and snippets.

@justinmc
Last active October 28, 2016 23:27
Show Gist options
  • Select an option

  • Save justinmc/22e170d07bdd84eeefd096027db42a1f to your computer and use it in GitHub Desktop.

Select an option

Save justinmc/22e170d07bdd84eeefd096027db42a1f to your computer and use it in GitHub Desktop.
Benchmark random access of an object, with keys created as random hash strings
const md5 = require('md5');
const size = parseInt(process.argv[2], 10);
if (!size) {
throw new Error('Must provide size of object as first argument.');
}
console.log(`Initializing object of size ${size}...`);
const obj = {};
for (let i = 0; i < size; i += 1) {
obj[md5(i)] = i;
}
console.log('Completed initialization.');
console.log('Running benchmark...');
const key = md5(Math.floor(Math.random() * size));
const startTime = Date.now();
const value = obj[key];
console.log(`Completed benchmark, found ${value} at ${key} in ${Date.now() - startTime}ms.`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment