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 +3 -0
- package/README.md +16 -16
- package/index.cjs +19 -11
- package/index.js +19 -11
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
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
|
87
|
-
customAlphabet
|
88
|
-
uid.sync
|
89
|
-
uuid v4
|
90
|
-
secure-random-string
|
91
|
-
cuid
|
92
|
-
shortid
|
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
|
96
|
-
async customAlphabet
|
97
|
-
async secure-random-string
|
98
|
-
uid
|
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,
|
102
|
-
rndm 2,
|
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-
|
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() //=> "
|
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
|
-
//
|
6
|
-
//
|
7
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
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
|
-
//
|
6
|
-
//
|
7
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
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) => {
|