nanoid
Advanced tools
Comparing version 3.1.15 to 3.1.16
# Change Log | ||
This project adheres to [Semantic Versioning](http://semver.org/). | ||
## 3.1.16 | ||
* Speed up Nano ID 4 times (by Peter Boyer). | ||
## 3.1.15 | ||
@@ -5,0 +8,0 @@ * Fixed `package.types` path. |
30
index.js
@@ -5,15 +5,23 @@ import crypto from 'crypto' | ||
// We reuse buffers with the same size to avoid memory fragmentations | ||
// for better performance. | ||
let buffers = {} | ||
// It is best to make fewer, larger requests to the crypto module to | ||
// avoid system call overhead. So, random numbers are generated in a | ||
// pool. The pool is a Buffer that is larger than the initial random | ||
// request size by this multiplier. The pool is enlarged if subsequent | ||
// requests exceed the maximum buffer size. | ||
const POOL_SIZE_MULTIPLIER = 32 | ||
let pool, poolOffset | ||
let random = bytes => { | ||
let buffer = buffers[bytes] | ||
if (!buffer) { | ||
// `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory. | ||
// Memory flushing is unnecessary since the buffer allocation itself resets | ||
// the memory with the new bytes. | ||
buffer = Buffer.allocUnsafe(bytes) | ||
if (bytes <= 255) buffers[bytes] = buffer | ||
if (!pool || pool.length < bytes) { | ||
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER) | ||
crypto.randomFillSync(pool) | ||
poolOffset = 0 | ||
} else if (poolOffset + bytes > pool.length) { | ||
crypto.randomFillSync(pool) | ||
poolOffset = 0 | ||
} | ||
return crypto.randomFillSync(buffer) | ||
let res = pool.subarray(poolOffset, poolOffset + bytes) | ||
poolOffset += bytes | ||
return res | ||
} | ||
@@ -20,0 +28,0 @@ |
{ | ||
"name": "nanoid", | ||
"version": "3.1.15", | ||
"version": "3.1.16", | ||
"description": "A tiny (108 bytes), secure URL-friendly unique string ID generator", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -85,23 +85,23 @@ # Nano ID | ||
```rust | ||
$ ./test/benchmark | ||
nanoid 655,798 ops/sec | ||
customAlphabet 635,421 ops/sec | ||
uid.sync 375,816 ops/sec | ||
uuid v4 396,756 ops/sec | ||
secure-random-string 366,434 ops/sec | ||
cuid 183,998 ops/sec | ||
shortid 59,343 ops/sec | ||
$ node ./test/benchmark.js | ||
nanoid 2,280,683 ops/sec | ||
customAlphabet 1,851,117 ops/sec | ||
uid.sync 313,306 ops/sec | ||
uuid v4 1,348,425 ops/sec | ||
secure-random-string 294,161 ops/sec | ||
cuid 158,988 ops/sec | ||
shortid 37,222 ops/sec | ||
Async: | ||
async nanoid 101,966 ops/sec | ||
async customAlphabet 102,471 ops/sec | ||
async secure-random-string 97,206 ops/sec | ||
uid 91,291 ops/sec | ||
async nanoid 95,500 ops/sec | ||
async customAlphabet 93,800 ops/sec | ||
async secure-random-string 90,316 ops/sec | ||
uid 85,583 ops/sec | ||
Non-secure: | ||
non-secure nanoid 2,754,423 ops/sec | ||
rndm 2,437,262 ops/sec | ||
non-secure nanoid 2,641,654 ops/sec | ||
rndm 2,447,086 ops/sec | ||
``` | ||
Test configuration: Dell XPS 2-in-a 7390, Fedora 32, Node.js 13.11. | ||
Test configuration: Dell XPS 2-in-1 7390, Fedora 32, Node.js 15.1. | ||
@@ -155,3 +155,3 @@ | ||
import { nanoid } from 'nanoid' | ||
model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ" | ||
model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT" | ||
``` | ||
@@ -158,0 +158,0 @@ |
Sorry, the diff of this file is not supported yet
49854
706