-
-
Notifications
You must be signed in to change notification settings - Fork 827
Reduce size by new url generation and counter reuse #170
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
Changes from all commits
54c5961
bd49e7e
dbe1555
df8ccf1
1d7ef8b
6c6c0c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not? i = size || 21
var bytes = crypto.getRandomValues(new Uint8Array(i))
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For better gzip compression. It makes +1 byte. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really? o:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I was wrong. +3 bytes :D There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.