diff --git a/.npmignore b/.npmignore index a1946909..387fcfc1 100644 --- a/.npmignore +++ b/.npmignore @@ -13,3 +13,5 @@ coverage/ .yaspellerrc docs/ + +benchmark diff --git a/benchmark b/benchmark new file mode 100755 index 00000000..0319be05 --- /dev/null +++ b/benchmark @@ -0,0 +1,13 @@ +#!/usr/bin/env node + +var nanoid = require('./') +var generate = require('./generate') + +var start = Date.now() +for (var i = 0; i < 100000; i++) { + nanoid() + generate('1234567890abcdef', 10) +} +var end = Date.now() + +process.stdout.write(end - start + ' ms\n') diff --git a/format.js b/format.js index d3d43520..4c0f0d53 100644 --- a/format.js +++ b/format.js @@ -24,10 +24,12 @@ * @name format */ module.exports = function (random, alphabet, size) { + var step = Math.ceil(1.2 * 256 / alphabet.length * size) + var bytes, byte var id = '' while (id.length !== size) { - bytes = random(size) + bytes = random(step) for (var i = 0; i < bytes.length; i++) { byte = bytes[i] if (byte < alphabet.length) { @@ -36,6 +38,7 @@ module.exports = function (random, alphabet, size) { } } } + return id }