nanoid 3.1.15 → 3.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of nanoid might be problematic. Click here for more details.

package/CHANGELOG.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # Change Log
2
2
  This project adheres to [Semantic Versioning](http://semver.org/).
3
3
 
4
+ ## 3.1.16
5
+ * Speed up Nano ID 4 times (by Peter Boyer).
6
+
4
7
  ## 3.1.15
5
8
  * Fixed `package.types` path.
6
9
 
package/README.md CHANGED
@@ -82,27 +82,27 @@ There are three main differences between Nano ID and UUID v4:
82
82
  ## Benchmark
83
83
 
84
84
  ```rust
85
- $ ./test/benchmark
86
- nanoid 655,798 ops/sec
87
- customAlphabet 635,421 ops/sec
88
- uid.sync 375,816 ops/sec
89
- uuid v4 396,756 ops/sec
90
- secure-random-string 366,434 ops/sec
91
- cuid 183,998 ops/sec
92
- shortid 59,343 ops/sec
85
+ $ node ./test/benchmark.js
86
+ nanoid 2,280,683 ops/sec
87
+ customAlphabet 1,851,117 ops/sec
88
+ uid.sync 313,306 ops/sec
89
+ uuid v4 1,348,425 ops/sec
90
+ secure-random-string 294,161 ops/sec
91
+ cuid 158,988 ops/sec
92
+ shortid 37,222 ops/sec
93
93
 
94
94
  Async:
95
- async nanoid 101,966 ops/sec
96
- async customAlphabet 102,471 ops/sec
97
- async secure-random-string 97,206 ops/sec
98
- uid 91,291 ops/sec
95
+ async nanoid 95,500 ops/sec
96
+ async customAlphabet 93,800 ops/sec
97
+ async secure-random-string 90,316 ops/sec
98
+ uid 85,583 ops/sec
99
99
 
100
100
  Non-secure:
101
- non-secure nanoid 2,754,423 ops/sec
102
- rndm 2,437,262 ops/sec
101
+ non-secure nanoid 2,641,654 ops/sec
102
+ rndm 2,447,086 ops/sec
103
103
  ```
104
104
 
105
- Test configuration: Dell XPS 2-in-a 7390, Fedora 32, Node.js 13.11.
105
+ Test configuration: Dell XPS 2-in-1 7390, Fedora 32, Node.js 15.1.
106
106
 
107
107
 
108
108
  ## Tools
@@ -151,7 +151,7 @@ with 21 characters (to have a collision probability similar to UUID v4).
151
151
 
152
152
  ```js
153
153
  import { nanoid } from 'nanoid'
154
- model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
154
+ model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
155
155
  ```
156
156
 
157
157
  If you want to reduce the ID size (and increase collisions probability),
package/index.cjs CHANGED
@@ -2,19 +2,27 @@ let crypto = require('crypto')
2
2
 
3
3
  let { urlAlphabet } = require('./url-alphabet/index.cjs')
4
4
 
5
- // We reuse buffers with the same size to avoid memory fragmentations
6
- // for better performance.
7
- let buffers = {}
5
+ // It is best to make fewer, larger requests to the crypto module to
6
+ // avoid system call overhead. So, random numbers are generated in a
7
+ // pool. The pool is a Buffer that is larger than the initial random
8
+ // request size by this multiplier. The pool is enlarged if subsequent
9
+ // requests exceed the maximum buffer size.
10
+ const POOL_SIZE_MULTIPLIER = 32
11
+ let pool, poolOffset
12
+
8
13
  let random = bytes => {
9
- let buffer = buffers[bytes]
10
- if (!buffer) {
11
- // `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
12
- // Memory flushing is unnecessary since the buffer allocation itself resets
13
- // the memory with the new bytes.
14
- buffer = Buffer.allocUnsafe(bytes)
15
- if (bytes <= 255) buffers[bytes] = buffer
14
+ if (!pool || pool.length < bytes) {
15
+ pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
16
+ crypto.randomFillSync(pool)
17
+ poolOffset = 0
18
+ } else if (poolOffset + bytes > pool.length) {
19
+ crypto.randomFillSync(pool)
20
+ poolOffset = 0
16
21
  }
17
- return crypto.randomFillSync(buffer)
22
+
23
+ let res = pool.subarray(poolOffset, poolOffset + bytes)
24
+ poolOffset += bytes
25
+ return res
18
26
  }
19
27
 
20
28
  let customRandom = (alphabet, size, getRandom) => {
package/index.js CHANGED
@@ -2,19 +2,27 @@ import crypto from 'crypto'
2
2
 
3
3
  import { urlAlphabet } from './url-alphabet/index.js'
4
4
 
5
- // We reuse buffers with the same size to avoid memory fragmentations
6
- // for better performance.
7
- let buffers = {}
5
+ // It is best to make fewer, larger requests to the crypto module to
6
+ // avoid system call overhead. So, random numbers are generated in a
7
+ // pool. The pool is a Buffer that is larger than the initial random
8
+ // request size by this multiplier. The pool is enlarged if subsequent
9
+ // requests exceed the maximum buffer size.
10
+ const POOL_SIZE_MULTIPLIER = 32
11
+ let pool, poolOffset
12
+
8
13
  let random = bytes => {
9
- let buffer = buffers[bytes]
10
- if (!buffer) {
11
- // `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
12
- // Memory flushing is unnecessary since the buffer allocation itself resets
13
- // the memory with the new bytes.
14
- buffer = Buffer.allocUnsafe(bytes)
15
- if (bytes <= 255) buffers[bytes] = buffer
14
+ if (!pool || pool.length < bytes) {
15
+ pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
16
+ crypto.randomFillSync(pool)
17
+ poolOffset = 0
18
+ } else if (poolOffset + bytes > pool.length) {
19
+ crypto.randomFillSync(pool)
20
+ poolOffset = 0
16
21
  }
17
- return crypto.randomFillSync(buffer)
22
+
23
+ let res = pool.subarray(poolOffset, poolOffset + bytes)
24
+ poolOffset += bytes
25
+ return res
18
26
  }
19
27
 
20
28
  let customRandom = (alphabet, size, getRandom) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "3.1.15",
3
+ "version": "3.1.16",
4
4
  "description": "A tiny (108 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",