Last active
October 28, 2016 23:27
-
-
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
This file contains hidden or 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 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