Skip to content

Commit

Permalink
reduce bundlesize by moving from while to reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
artalar committed Apr 18, 2022
1 parent cb3053e commit a584cfd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
15 changes: 5 additions & 10 deletions index.browser.js
Expand Up @@ -47,18 +47,14 @@ let customRandom = (alphabet, defaultSize, getRandom) => {
let customAlphabet = (alphabet, size = 21) =>
customRandom(alphabet, size, random)

let nanoid = (size = 21) => {
let id = ''
let bytes = crypto.getRandomValues(new Uint8Array(size))

// A compact alternative for `for (var i = 0; i < step; i++)`.
while (size--) {
let nanoid = (size = 21) =>
crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
// It is incorrect to use bytes exceeding the alphabet size.
// The following mask reduces the random byte in the 0-255 value
// range to the 0-63 value range. Therefore, adding hacks, such
// as empty string fallback or magic numbers, is unneccessary because
// the bitmask trims bytes down to the alphabet size.
let byte = bytes[size] & 63
byte &= 63
if (byte < 36) {
// `0-9a-z`
id += byte.toString(36)
Expand All @@ -70,8 +66,7 @@ let nanoid = (size = 21) => {
} else {
id += '-'
}
}
return id
}
return id
}, '')

module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
2 changes: 1 addition & 1 deletion nanoid.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "nanoid",
"version": "3.3.2",
"description": "A tiny (130 bytes), secure URL-friendly unique string ID generator",
"description": "A tiny (116 bytes), secure URL-friendly unique string ID generator",
"keywords": [
"uuid",
"random",
Expand Down Expand Up @@ -68,7 +68,7 @@
{
"name": "nanoid",
"import": "{ nanoid }",
"limit": "130 B"
"limit": "116 B"
},
{
"name": "customAlphabet",
Expand Down Expand Up @@ -108,7 +108,7 @@
"name": "Brotli all",
"brotli": true,
"import": "{ nanoid, customAlphabet, urlAlphabet }",
"limit": "271 B"
"limit": "264 B"
},
{
"name": "Brotli non-secure",
Expand Down

0 comments on commit a584cfd

Please sign in to comment.