Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce size by new url generation and counter reuse #170

Merged
merged 6 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions async/index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,38 @@
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 i
var 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 = 36
gwer marked this conversation as resolved.
Show resolved Hide resolved
while (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)
gwer marked this conversation as resolved.
Show resolved Hide resolved
}

// 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) {
gwer marked this conversation as resolved.
Show resolved Hide resolved
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
Comment on lines +29 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not?

  i = size || 21
  var bytes = crypto.getRandomValues(new Uint8Array(i))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better gzip compression. It makes +1 byte.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really? o:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was wrong. +3 bytes :D

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha


// 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)
}
45 changes: 22 additions & 23 deletions index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,38 @@ if (process.env.NODE_ENV !== 'production') {
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 i
var 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 = 36
while (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
}
40 changes: 19 additions & 21 deletions non-secure/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
// 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
var 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 = 36
while (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
Expand All @@ -37,10 +35,10 @@ var url = '_-' + String.fromCharCode(
* @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]
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"size-limit": [
{
"path": "index.js",
"limit": "127 B"
"limit": "119 B"
},
{
"path": "generate.js",
Expand All @@ -78,15 +78,15 @@
},
{
"path": "non-secure/index.js",
"limit": "89 B"
"limit": "82 B"
},
{
"path": "non-secure/generate.js",
"limit": "45 B"
},
{
"path": "async/index.js",
"limit": "139 B"
"limit": "130 B"
},
{
"path": "async/generate.js",
Expand Down
28 changes: 10 additions & 18 deletions url.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// is low and there are lots of duplicates - just what compressors
// like GZIP and Brotli likes the best.

var i

/**
* URL safe symbols.
*
Expand All @@ -15,20 +13,14 @@ var i
* 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 = 36
while (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())
}