Speed up ID generation by binary mask
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent bb3d6ca · Browse files · 4 files·+37 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+37 added−17 removed
- other:+37 added−17 removed
Provenance
- Commit
d6ad341- Tree
a2afdcb- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
bb3d6ca
4 files changed+37 added−17 removed
modified:format.js+9 added-3 removed
12345618 unmodified lines25262728272930313233343536373334383940414243447 unmodified linesvar masks = [15, 31, 63, 127, 255]
/** * Secure random string generator with custom alphabet. * * Alphabet must contain 256 symbols or less. Otherwise, the generator18 unmodified lines * * @name format */module.exports = function (random, alphabet, size) { var step = Math.ceil(310 / alphabet.length * size) var mask = masks.find(function (i) { return i >= alphabet.length - 1 }) var step = Math.ceil(1.6 * mask * size / alphabet.length)
var id = '' while (true) { var bytes = random(step) for (var i = 0; i < step; i++) { if (bytes[i] < alphabet.length) { id += alphabet[bytes[i]] var byte = bytes[i] & mask if (alphabet[byte]) { id += alphabet[byte] if (id.length === size) return id } } }7 unmodified linesmodified:index.js+16 added-5 removed
11323456788910111213141516171819201617212223242526272829var format = require('./format')var random = require('./random')var url = require('./url')
var url = '_~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
/** * Generate secure URL-friendly unique ID. * * @return {string} Random string with 22 URL-friendly symbols. * By default, ID will have 22 symbols to have same collisions probability * as UUID v4. * * @param {number} [size=22] The number of symbols in ID. * * @return {string} Random string. * * @example * var nanoid = require('nanoid') * model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ" * * @name nanoid */module.exports = function () { return format(random, url, 22)module.exports = function (size) { size = size || 22 var id = '' var bytes = random(size) for (var i = 0; i < size; i++) { id += url[bytes[i] & 63] } return id}modified:package.json+5 added-1 removed
59 unmodified lines60616263646465666768697071726 unmodified lines59 unmodified lines }, "size-limit": [ { "path": "index.js", "limit": "238 B" "limit": "181 B" }, { "path": "generate.js", "limit": "197 B" } ], "lint-staged": { "*.md": "yaspeller-ci",6 unmodified linesmodified:README.md+7 added-8 removed
78 unmodified lines79 80 Symbols `-,.()` are not encoded in URL, but in the end of a link81 they could be identified as a punctuation symbol.82 83+If you want to reduce ID length (and increase collisions probability),84+you can pass length as argument:85+86+```js87+nanoid(10) //=> "IRFa~VaY2b"88+```89+90 ### Custom Alphabet or Length91 92 If you want to change the ID alphabet or the length93 you can use low-level `generate` module.4 unmodified lines98 ```99 100 Alphabet must contain less than 256 symbols.101 95-If you want to use the same URL-friendly symbols and just change the length,96-you can get default alphabet from the `url` module:97-98-```js99-var url = require('nanoid/url')100-model.id = generate(url, 10) //=> "Uakgb_J5m9"101-```102-102 ### Custom Random Bytes Generator103 104 You can replace the default safe random generator using the `format` module.105 For instance, to use seed-based generator.23 unmodified lines