Reduce size by new url generation and counter reuse (#170)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Reduce size by url generation and getRandomValues in loop * Update non-secure index.js * Update url.js * Single call crypto.getRandomValues * Update comments * A-Z => a-z
authored by Vladlen Grachev · committed by GitHub · parent dfef624 · Browse files · 5 files·+76 added−88 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+76 added−88 removed
- other:+76 added−88 removed
Provenance
- Commit
9c080ef- Tree
eecd8fb- Author
Vladlen Grachev <webholt@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
dfef624
5 files changed+76 added−88 removed
modified:async/index.browser.js+22 added-23 removed
2 unmodified lines3456789101112131478910111617181920121314151617181920222324252627212223242526273028322930313234333435363739383940412 unmodified lines
var crypto = self.crypto || self.msCrypto
// This alphabet uses a-z A-Z 0-9 _- symbols.// Despite the fact the source code is quite long, its entropy// is low and there are lots of duplicates - just what compressors// like GZIP and Brotli likes the best.var ivar url = '_-' + String.fromCharCode( // ASCII codes for 0...9 i = 48, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,// Symbols are generated for better gzip compression.// Final url is// '-_zyxwvutsrqponmlkjihgfedcba9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA'var url = '-_'
// ASCII codes for A...Z i += 8, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,var i = 36while (i--) { // 36 is radix. // Number.prototype.toString(36) returns number in Base36 representation. // Base36 is like hex, // but Base36 is represented using the numerals 0–9 and the Latin letters a-z url += i.toString(36)}
// ASCII codes for a...z i += 7, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1)i = 36// Loop from 36 to 10 (from Z to A in Base36)while (i-- - 10) { url += i.toString(36).toUpperCase()}
module.exports = function (size) { size = size || 21 var id = '' var bytes = crypto.getRandomValues(new Uint8Array(size)) var bytes = crypto.getRandomValues(new Uint8Array(size || 21)) i = size || 21
// Compact alternative for `for (var i = 0; i < size; i++)` while (size--) { while (i--) { // We can’t use bytes bigger than the alphabet. 63 is 00111111 bitmask. // This mask reduces random byte 0-255 to 0-63 values. // There is no need in `|| ''` and `* 1.6` hacks in here, // because bitmask trim bytes exact to alphabet size. id += url[bytes[size] & 63] id += url[bytes[i] & 63] } return Promise.resolve(id)}modified:non-secure/index.js+19 added-21 removed
1234567892345611121314157891011121314151718192021221617181920212223249 unmodified lines343536374038394043414243444546// This alphabet uses a-z A-Z 0-9 _- symbols.// Despite the fact the source code is quite long, its entropy// is low and there are lots of duplicates - just what compressors// like GZIP and Brotli likes the best.var ivar url = '_-' + String.fromCharCode( // ASCII codes for 0...9 i = 48, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,// Symbols are generated for better gzip compression.// Final url is// '-_zyxwvutsrqponmlkjihgfedcba9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA'var url = '-_'
// ASCII codes for A...Z i += 8, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,var i = 36while (i--) { // 36 is radix. // Number.prototype.toString(36) returns number in Base36 representation. // Base36 is like hex, // but Base36 is represented using the numerals 0–9 and the Latin letters a-z url += i.toString(36)}
// ASCII codes for a...z i += 7, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1)i = 36// Loop from 36 to 10 (from Z to A in Base36)while (i-- - 10) { url += i.toString(36).toUpperCase()}
/** * Generate URL-friendly unique ID. This method use non-secure predictable * random generator with bigger collision probability.9 unmodified lines * @name nonSecure * @function */module.exports = function (size) { size = size || 21 var id = '' i = size || 21 // Compact alternative for `for (var i = 0; i < size; i++)` while (size--) { while (i--) { // `| 0` is compact and faster alternative for `Math.floor()` id += url[Math.random() * 64 | 0] } return id}modified:index.browser.js+22 added-23 removed
19 unmodified lines202122232425262728293031242526272833343536372930313233343536373940414243443839404142434447454946474849515051525354565556575819 unmodified lines
var crypto = self.crypto || self.msCrypto
// This alphabet uses a-z A-Z 0-9 _- symbols.// Despite the fact the source code is quite long, its entropy// is low and there are lots of duplicates - just what compressors// like GZIP and Brotli likes the best.var ivar url = '_-' + String.fromCharCode( // ASCII codes for 0...9 i = 48, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,// Symbols are generated for better gzip compression.// Final url is// '-_zyxwvutsrqponmlkjihgfedcba9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA'var url = '-_'
// ASCII codes for A...Z i += 8, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,var i = 36while (i--) { // 36 is radix. // Number.prototype.toString(36) returns number in Base36 representation. // Base36 is like hex, // but Base36 is represented using the numerals 0–9 and the Latin letters a-z url += i.toString(36)}
// ASCII codes for a...z i += 7, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1)i = 36// Loop from 36 to 10 (from Z to A in Base36)while (i-- - 10) { url += i.toString(36).toUpperCase()}
module.exports = function (size) { size = size || 21 var id = '' var bytes = crypto.getRandomValues(new Uint8Array(size)) var bytes = crypto.getRandomValues(new Uint8Array(size || 21)) i = size || 21
// Compact alternative for `for (var i = 0; i < size; i++)` while (size--) { while (i--) { // We can’t use bytes bigger than the alphabet. 63 is 00111111 bitmask. // This mask reduces random byte 0-255 to 0-63 values. // There is no need in `|| ''` and `* 1.6` hacks in here, // because bitmask trim bytes exact to alphabet size. id += url[bytes[size] & 63] id += url[bytes[i] & 63] } return id}modified:package.json+3 added-3 removed
64 unmodified lines65 },66 "size-limit": [67 {68 "path": "index.js",69- "limit": "127 B"69+ "limit": "119 B"70 },71 {72 "path": "generate.js",73 "limit": "168 B"3 unmodified lines77 "limit": "47 B"78 },79 {80 "path": "non-secure/index.js",81- "limit": "89 B"81+ "limit": "82 B"82 },83 {84 "path": "non-secure/generate.js",85 "limit": "45 B"86 },87 {88 "path": "async/index.js",89- "limit": "139 B"89+ "limit": "130 B"90 },91 {92 "path": "async/generate.js",93 "limit": "193 B"78 unmodified linesmodified:url.js+10 added-18 removed
123456767892 unmodified lines12131415181920211623242526272829303132333417181920212223242526// This alphabet uses a-z A-Z 0-9 _- symbols.// Despite the fact the source code is quite long, its entropy// is low and there are lots of duplicates - just what compressors// like GZIP and Brotli likes the best.
var i
/** * URL safe symbols. * * @name url2 unmodified lines * @example * const url = require('nanoid/url') * generate(url, 10) //=> "Uakgb_J5m9" */module.exports = '_-' + String.fromCharCode( // ASCII codes for 0...9 i = 48, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
// ASCII codes for A...Z i += 8, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
// ASCII codes for a...z i += 7, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1)module.exports = '-_'var i = 36while (i--) { // 36 is radix. // Number.prototype.toString(36) returns number in Base36 representation. // Base36 is like hex, // but Base36 is represented using the numerals 0–9 and the Latin letters a-z module.exports += i.toString(36) i > 9 && (module.exports += i.toString(36).toUpperCase())}