Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ai/nanoid
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2.1.10
Choose a base ref
...
head repository: ai/nanoid
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2.1.11
Choose a head ref
  • 5 commits
  • 8 files changed
  • 2 contributors

Commits on Jan 30, 2020

  1. Reduce size by new url generation and counter reuse (#170)

    * 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
    gwer authored Jan 30, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    9c080ef View commit details
  2. Update size in docs

    ai committed Jan 30, 2020
    Copy the full SHA
    ed7c15f View commit details
  3. Update dependencies

    ai committed Jan 30, 2020
    Copy the full SHA
    f66faaa View commit details
  4. Clean up comments

    ai committed Jan 30, 2020
    Copy the full SHA
    a4e2415 View commit details
  5. Release 2.1.11 version

    ai committed Jan 30, 2020
    Copy the full SHA
    7620aed View commit details
Showing with 299 additions and 312 deletions.
  1. +3 −0 CHANGELOG.md
  2. +2 −2 README.md
  3. +20 −25 async/index.browser.js
  4. +20 −25 index.browser.js
  5. +17 −23 non-secure/index.js
  6. +8 −8 package.json
  7. +11 −18 url.js
  8. +218 −211 yarn.lock
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Change Log
This project adheres to [Semantic Versioning](http://semver.org/).

## 2.1.11
* Reduce size (by Anton Evzhakov).

## 2.1.10
* Reduce size by 10% (by Anton Khlynovskiy).

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

A tiny, secure, URL-friendly, unique string ID generator for JavaScript.

* **Small.** 127 bytes (minified and gzipped). No dependencies.
* **Small.** 119 bytes (minified and gzipped). No dependencies.
[Size Limit] controls the size.
* **Safe.** It uses cryptographically strong random APIs.
Can be used in clusters.
@@ -63,7 +63,7 @@ There are three main differences between Nano ID and UUID v4:
1. Nano ID uses a bigger alphabet, so a similar number of random bits
are packed in just 21 symbols instead of 36.
2. Nano ID code is 4 times less than `uuid/v4` package:
127 bytes instead of 435.
119 bytes instead of 435.
3. Because of memory allocation tricks, Nano ID is 16% faster than UUID.


45 changes: 20 additions & 25 deletions async/index.browser.js
Original file line number Diff line number Diff line change
@@ -4,39 +4,34 @@
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,

// 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
)
// Symbols are generated for smaller size.
// -_zyxwvutsrqponmlkjihgfedcba9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA
var url = '-_'
// Loop from 36 to 0 (from z to a and 9 to 0 in Base36).
var i = 36
while (i--) {
// 36 is radix. Number.prototype.toString(36) returns number
// in Base36 representation. Base36 is like hex, but it uses 0–9 and a-z.
url += i.toString(36)
}
// Loop from 36 to 10 (from Z to A in Base36).
i = 36
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)
}
45 changes: 20 additions & 25 deletions index.browser.js
Original file line number Diff line number Diff line change
@@ -21,39 +21,34 @@ 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,

// 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
)
// Symbols are generated for smaller size.
// -_zyxwvutsrqponmlkjihgfedcba9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA
var url = '-_'
// Loop from 36 to 0 (from z to a and 9 to 0 in Base36).
var i = 36
while (i--) {
// 36 is radix. Number.prototype.toString(36) returns number
// in Base36 representation. Base36 is like hex, but it uses 0–9 and a-z.
url += i.toString(36)
}
// Loop from 36 to 10 (from Z to A in Base36).
i = 36
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: 17 additions & 23 deletions non-secure/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
// 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,

// 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
)
// Symbols are generated for smaller size.
// -_zyxwvutsrqponmlkjihgfedcba9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA
var url = '-_'
// Loop from 36 to 0 (from z to a and 9 to 0 in Base36).
var i = 36
while (i--) {
// 36 is radix. Number.prototype.toString(36) returns number
// in Base36 representation. Base36 is like hex, but it uses 0–9 and a-z.
url += i.toString(36)
}
// Loop from 36 to 10 (from Z to A in Base36).
i = 36
while (i-- - 10) {
url += i.toString(36).toUpperCase()
}

/**
* Generate URL-friendly unique ID. This method use non-secure predictable
@@ -37,10 +31,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]
}
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nanoid",
"version": "2.1.10",
"description": "A tiny (127 bytes), secure URL-friendly unique string ID generator",
"version": "2.1.11",
"description": "A tiny (119 bytes), secure URL-friendly unique string ID generator",
"keywords": [
"uuid",
"random",
@@ -50,11 +50,11 @@
"eslint-plugin-security": "^1.4.0",
"eslint-plugin-standard": "^4.0.1",
"eslint-plugin-unicorn": "^15.0.1",
"husky": "^4.2.0",
"husky": "^4.2.1",
"jest": "^25.1.0",
"lint-staged": "^10.0.1",
"lint-staged": "^10.0.6",
"parcel-bundler": "^1.12.4",
"rimraf": "^3.0.0",
"rimraf": "^3.0.1",
"rndm": "^1.2.0",
"secure-random-string": "^1.1.2",
"shortid": "^2.2.15",
@@ -66,7 +66,7 @@
"size-limit": [
{
"path": "index.js",
"limit": "127 B"
"limit": "119 B"
},
{
"path": "generate.js",
@@ -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",
29 changes: 11 additions & 18 deletions url.js
Original file line number Diff line number Diff line change
@@ -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.
*
@@ -15,20 +13,15 @@ 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
)
// This alphabet uses a-z A-Z 0-9 _- symbols.
// Symbols are generated for smaller size.
// -_zyxwvutsrqponmlkjihgfedcba9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA
module.exports = '-_'
var i = 36
while (i--) {
// 36 is radix. Number.prototype.toString(36) returns number
// in Base36 representation. Base36 is like hex, but it uses 0–9 and a-z.
module.exports += i.toString(36)
i > 9 && (module.exports += i.toString(36).toUpperCase())
}
429 changes: 218 additions & 211 deletions yarn.lock

Large diffs are not rendered by default.