Skip to content
Myr

Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Use full byte range in customAlphabet * Add comment to clarify bitmask usage for power-of-two alphabets in customRandom * Optimise customRandom function to avoid unneccesary if check at runtime * Update customAlphabet limit in package.json to 193 B * Document safeByteCutoff and mask usage in customRandom

authored by Denis Saripov · committed by GitHub · parent f4e7fbe · Browse files · 4 files+88 added−49 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+88 added−49 removed

  • tests:+5 added−2 removed
  • other:+83 added−47 removed

4 files+88 added−49 removed

modified:test/index.test.js+5 added-2 removed
139 unmodified lines
140141 test(`${type} / customAlphabet / is ready for 0 size`, () => {142 equal(customAlphabet('abc')(0), '')143 equal(customAlphabet('abc', 0)(0), '')144 equal(customAlphabet('')(0), '')145 equal(customAlphabet('', 0)(0), '')146 })147148 test(`${type} / customAlphabet / avoids pool pollution, infinite loop`, () => {149 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
14 unmodified lines
164 return bytes165 }166 let nanoid4 = customRandom('abcde', 4, fakeRandom)167 let nanoid18 = customRandom('abcde', 18, fakeRandom)166 equal(nanoid4(), 'adca')167 equal(nanoid18(), 'cbadcbadcbadcbadcc')168 equal(nanoid4(), 'cccc')169 equal(nanoid18(), 'acccccdcbacccccdcb')170 })171172 test(`${type} / customRandom / is ready for 0 size`, () => {173 let nanoid0 = customRandom('abc', 5, size => new Uint8Array(size))174 equal(nanoid0(0), '')175 equal(customRandom('', 5, size => new Uint8Array(size))(0), '')176 })177178 test(`${type} / urlAlphabet / is string`, () => {179 equal(typeof urlAlphabet, 'string')
70 unmodified lines
modified:index.browser.js+41 added-25 removed
modified:index.js+41 added-21 removed
modified:package.json+1 added-1 removed
86 unmodified lines
87 },88 {89 "name": "customAlphabet",90 "import": "{ customAlphabet }",91 "limit": "178 B"91 "limit": "193 B"92 },93 {94 "name": "urlAlphabet",95 "import": "{ urlAlphabet }",
32 unmodified lines