Add non-secure generator
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 889c1cc · Browse files · 6 files·+109 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+109 added−11 removed
- tests:+64 added−6 removed
- other:+45 added−5 removed
Provenance
- Commit
aa39d81- Tree
e6937f7- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
889c1cc
6 files changed+109 added−11 removed
modified:test/demo/index.js+8 added-2 removed
123456786 unmodified lines1516171818192021222315 unmodified lines3940414242434445464739 unmodified lines8788899091929394959697989928 unmodified linesvar shortid = require('shortid')var uuid4 = require('uuid/v4')
var nonSecure = require('../../non-secure')var generate = require('../../generate')var random = require('../../random')var nanoid = require('../../')
6 unmodified linesvar COUNT = 50 * 1000var ALPHABET = 'abcdefghijklmnopqrstuvwxyz'var LENGTH = ALPHABET.length
function format (number) {function print (number) { return String(Math.floor(number * 100)) .replace(/\d\d\d\d\d\d$/, ',$&') .replace(/\d\d\d$/, ',$&')}15 unmodified lines '">' + l + '</div>' })
document.body.innerHTML += '<section>' + '<span>' + format(COUNT * 1000 / data.time) + ' ops/sec</span>' + '<span>' + print(COUNT * 1000 / data.time) + ' ops/sec</span>' + '<h2>' + data.title + '</h2>' + dots + '</section>' })39 unmodified lines return nanoid() }) }, function () { return printDistribution('nanoid/non-secure', function () { return nonSecure() }) }, function () { return printDistribution('nanoid/generate', function () { return generate(ALPHABET, LENGTH) }) },28 unmodified linesmodified:test/benchmark+10 added-4 removed
2 unmodified lines3 var shortid = require('shortid')4 var uuid4 = require('uuid/v4')5 var chalk = require('chalk')6 7+var nonSecure = require('../non-secure')8 var generate = require('../generate')9 var nanoid = require('../')10 11 var suite = new benchmark.Suite()12 13 function formatNumber (number) {13- return String(number).replace(/\d\d\d$/, ',$&')14+ return String(number)15+ .replace(/\d\d\d$/, ',$&')16+ .replace(/^(\d)(\d\d\d)/, '$1,$2')17 }18 19 suite20 .add('nanoid', function () {21 nanoid()22 })23+ .add('nanoid/non-secure', function () {24+ nonSecure()25+ })26 .add('nanoid/generate', function () {27 generate('1234567890abcdef-', 10)28 })29 .add('uuid/v4', function () {2 unmodified lines32 .add('shortid', function () {33 shortid()34 })35 .on('cycle', function (event) {30- var name = event.target.name.padEnd('nanoid/generate'.length)31- var hz = formatNumber(event.target.hz.toFixed(0)).padStart(7)32- process.stdout.write(name + ' ' + chalk.bold(hz) + ' ops/sec \n')36+ var name = event.target.name.padEnd('nanoid/non-secure'.length)37+ var hz = formatNumber(event.target.hz.toFixed(0)).padStart(9)38+ process.stdout.write(name + ' ' + chalk.bold(hz) + ' ops/sec\n')39 })40 .run()added:test/non-secure.test.js+46 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546var nonSecure = require('../non-secure')var url = require('../url')
it('generates URL-friendly IDs', function () { for (var i = 0; i < 10; i++) { var id = nonSecure() expect(id).toHaveLength(21) for (var j = 0; j < id.length; j++) { expect(url.indexOf(id[j])).not.toEqual(-1) } }})
it('has no collisions', function () { var COUNT = 100 * 1000 var used = { } for (var i = 0; i < COUNT; i++) { var id = nonSecure() expect(used[id]).not.toBeDefined() used[id] = true }})
it('has flat distribution', function () { var COUNT = 100 * 1000 var LENGTH = nonSecure().length
var chars = { } for (var i = 0; i < COUNT; i++) { var id = nonSecure() for (var j = 0; j < id.length; j++) { var char = id[j] if (!chars[char]) chars[char] = 0 chars[char] += 1 } }
var max = 0 var min = Number.MAX_SAFE_INTEGER for (var k in chars) { var distribution = (chars[k] * url.length) / (COUNT * LENGTH) if (distribution > max) max = distribution if (distribution < min) min = distribution } expect(max - min).toBeLessThanOrEqual(0.05)})modified:.size-limit+4 added
4 unmodified lines5 },6 {7 "path": "generate.js",8 "limit": "179 B"9+ },10+ {11+ "path": "non-secure.js",12+ "limit": "135 B"13 }14 ]added:non-secure.js+19 added
12345678910111213141516171819var url = '_~getRandomVcryp0123456789bfhijklqsuvwxzABCDEFGHIJKLMNOPQSTUWXYZ'
function random (size) { var result = [] while (0 < size--) { result.push(Math.floor(Math.random() * 256)) } return result}
module.exports = function (size) { size = size || 21 var id = '' var bytes = random(size) while (0 < size--) { id += url[bytes[size] & 63] } return id}modified:README.md+22 added-5 removed
77 unmodified lines78 79 ## Benchmark80 81 ```82-$ ./benchmark83-nanoid 332,991 ops/sec84-nanoid/generate 326,636 ops/sec85-uuid/v4 315,344 ops/sec86-shortid 33,717 ops/sec82+$ ./test/benchmark83+nanoid 353,737 ops/sec84+nanoid/non-secure 2,043,665 ops/sec85+nanoid/generate 348,442 ops/sec86+uuid/v4 350,075 ops/sec87+shortid 85,107 ops/sec88 ```89 90 91 ## Usage23 unmodified lines115 116 [ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/117 118 119+### React Native and Web Workers120+121+React Native and Web Worker don’t have access to secure random generator.122+123+Security is important in ID, when ID should be unpredictable. For instance,124+in “access by URL” link generation.125+126+If you don’t need unpredictable IDs, but you need React Native127+or Web Workers support, you can use non-secure ID generator:128+129+```js130+var nanoid = require('nanoid/non-secure')131+model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"132+```133+134+135 ### Custom Alphabet or Length136 137 If you want to change the ID's alphabet or length138 you can use the low-level `generate` module.77 unmodified lines