Last active
January 19, 2018 20:42
-
-
Save kjin/321ce5514a0a393f35c6aa71ef038789 to your computer and use it in GitHub Desktop.
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 crypto = require('crypto'); | |
const [_bin, _script, lenStr, algo] = process.argv; | |
const array = []; | |
const length = Number(lenStr); | |
if (algo === 'crypto-random-fill') { // ~23100ms for length-10000000 array | |
const bytes = Buffer.alloc(6); | |
const coeffs = [40, 32, 24, 16, 8].map(a => Math.pow(2, a)); | |
for (let i = 0; i < length; i++) { | |
crypto.randomFillSync(bytes); | |
array.push(String( | |
bytes[0] * coeffs[0] + | |
bytes[1] * coeffs[1] + | |
bytes[2] * coeffs[2] + | |
bytes[3] * coeffs[3] + | |
bytes[4] * coeffs[4] + | |
bytes[5])); | |
} | |
} else if (algo === 'crypto-random-bytes') { // ~27700ms for length-10000000 array | |
const coeffs = [40, 32, 24, 16, 8].map(a => Math.pow(2, a)); | |
for (let i = 0; i < length; i++) { | |
const bytes = crypto.randomBytes(6); | |
array.push(String( | |
bytes[0] * coeffs[0] + | |
bytes[1] * coeffs[1] + | |
bytes[2] * coeffs[2] + | |
bytes[3] * coeffs[3] + | |
bytes[4] * coeffs[4] + | |
bytes[5])); | |
} | |
} else if (algo === 'math-random-max-safe-floor') { // ~3800ms for length-10000000 array (6300ms without Math.floor) | |
for (let i = 0; i < length; i++) { | |
array.push(String(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER))); | |
} | |
} else if (algo === 'math-random-1e64') { // ~4200ms for length-10000000 array | |
for (let i = 0; i < length; i++) { | |
array.push(String(Math.floor(Math.random() * 18446744073709550000))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: I screwed up a little bit (coeffs
pre-map should have multiples of 8 instead)... fixing