Skip to content

Commit 6e96a2c

Browse files
authored
Merge pull request #16 from jchook/15-ios-9
Fixes iOS 9 issue and improves benchmarks
2 parents 42f6bf7 + 114951c commit 6e96a2c

File tree

9 files changed

+135
-60
lines changed

9 files changed

+135
-60
lines changed

CHANGELOG renamed to CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
The user-friendly summaries of how this project evolved over
44
time.
55

6+
## 1.3.1 <small>- Jun 19, 2020</small>
7+
8+
- Fixes issue with iOS 9 ([#15](https://github.com/jchook/uuid-random/issues/15))
9+
- Improves benchmarks
10+
11+
612
## 1.3.0 <small>- Sept 14, 2019</small>
713

814
- Exposes `uuid.randomBytes()` so you can override it, e.g. with [nacl](https://github.com/dchest/tweetnacl-js#random-bytes-generation) ([#5](https://github.com/jchook/uuid-random/issues/5#issuecomment-442081338))

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ Compatible with almost all versions of:
3333

3434
## Performance
3535

36-
The included `benchmark.js` as well as [independent benchmarks](https://github.com/aarondcohen/benchmark-guid) rank this library as the _fastest_ pure JS UUID v4 generator available with cryptographically secure PRNG— almost **5x faster** than the most popular library.
36+
The included `benchmark.js` as well as [independent benchmarks](https://github.com/aarondcohen/benchmark-guid) rank this library as the _fastest_ pure JS UUID v4 generator available with cryptographically secure PRNG— almost **20x faster** than the most popular library (using latest NodeJS).
3737

3838
| npm package | performance |
3939
|-----------------|-----------------|
40-
| portable-uuid | 487k ops/sec |
41-
| uuid | 502k ops/sec |
42-
| id128 | 2.1M ops/sec |
43-
| **uuid-random** <small>(this)</small> | **2.7M ops/sec** |
40+
| portable-uuid | 354k ops/sec |
41+
| uuid | 474k ops/sec |
42+
| id128 | 6.0M ops/sec |
43+
| **uuid-random** <small>(this)</small> | **9.7M ops/sec** |
4444

45-
*Results above generated on a 4.20GHz Intel i7-7700K with Node 10.15.0*
45+
*Results above generated on a 4.20GHz Intel i7-7700K with Node v12.18.0*
4646

4747
## Why use UUID?
4848

benchmark.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

benchmark/benchmark.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<script src="node_modules/lodash/lodash.js"></script>
4+
<script src="node_modules/benchmark/benchmark.js"></script>
5+
<script src="../index.js"></script>
6+
</head>
7+
8+
<body>
9+
<script>
10+
var suite = new Benchmark.Suite()
11+
console.log('Starting benchmark...')
12+
suite
13+
.add('uuid-random', uuid)
14+
.on('cycle', function (event) {
15+
console.log(String(event.target))
16+
})
17+
.on('complete', function () {
18+
console.log('Fastest is ' + this.filter('fastest').map('name'))
19+
})
20+
.run({ async: true })
21+
</script>
22+
</body>
23+
</html>

benchmark/benchmark.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*
3+
* Benchmark using the `benchmark` module.
4+
*
5+
* Unfortunately this module somehow adds overhead to the function calls
6+
* so the true ops/sec cannot be determined.
7+
*
8+
*/
9+
10+
var Benchmark = require('benchmark')
11+
var suite = new Benchmark.Suite()
12+
var id128 = require('id128')
13+
14+
suite
15+
.add('uuid-random', require('..'))
16+
.add('id128', function () { id128.Uuid4.generate().toCanonical() })
17+
.add('portable-uuid', require('portable-uuid'))
18+
.add('uuid', require('uuid').v4)
19+
.add('nanoid', require('nanoid').nanoid)
20+
.on('cycle', function (event) {
21+
console.log(String(event.target))
22+
})
23+
.on('complete', function () {
24+
console.log('Fastest is ' + this.filter('fastest').map('name'))
25+
})
26+
.run({ async: true })
27+
28+

benchmark/dispersion.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<html>
2+
<body>
3+
<script type="text/javascript">
4+
5+
const HEIGHT = 250;
6+
const WIDTH = 250;
7+
const ITERS = 250000;
8+
const AREA = HEIGHT * WIDTH;
9+
10+
function cryptoRandom() {
11+
// Divide a random UInt32 by the maximum value (2^32 -1) to get a result between 0 and 1
12+
return window.crypto.getRandomValues(new Uint32Array(1))[0] / 4294967295;
13+
}
14+
15+
function randomCoords(rnd) {
16+
var num = Math.floor(rnd() * AREA);
17+
var xx = Math.floor(num / WIDTH);
18+
var yy = num % HEIGHT;
19+
return [xx, yy];
20+
}
21+
22+
function doRender(id, rnd) {
23+
var h1 = document.createElement('h1');
24+
h1.innerHTML = id;
25+
document.body.appendChild(h1);
26+
var canvas = document.createElement('canvas');
27+
document.body.appendChild(canvas);
28+
var ctx = canvas.getContext('2d');
29+
canvas.setAttribute('height', HEIGHT);
30+
canvas.setAttribute('width', WIDTH);
31+
32+
var xx, yy, ii;
33+
34+
for (var ii = 0; ii < ITERS; ii++) {
35+
36+
[xx, yy] = randomCoords(rnd);
37+
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
38+
ctx.fillRect(xx, yy, 1, 1);
39+
}
40+
}
41+
42+
doRender('mathRandom', Math.random);
43+
doRender('cryptoRandom', cryptoRandom);
44+
</script>
45+
</body>
46+
</html>

benchmark/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "uuid-random-benchmark",
3+
"version": "1.0.0",
4+
"description": "Benchmarking various uuid libraries",
5+
"main": "index.js",
6+
"author": "Wes Roberts",
7+
"license": "MIT",
8+
"dependencies": {
9+
"benchmark": "^2.1.4",
10+
"fast-stats": "^0.0.5",
11+
"id128": "^1.5.0",
12+
"microtime": "^3.0.0",
13+
"nanoid": "^3.1.10",
14+
"platform.js": "^1.0.0",
15+
"portable-uuid": "^0.3.0",
16+
"uuid": "^8.1.0"
17+
}
18+
}

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@
5959
return crypt0.randomBytes;
6060
}
6161
if (crypt0.getRandomValues) {
62+
if (typeof Uint8Array.prototype.slice !== 'function') {
63+
return function(n) {
64+
var bytes = new Uint8Array(n);
65+
crypt0.getRandomValues(bytes);
66+
return Array.from(bytes);
67+
};
68+
}
6269
return function(n) {
6370
var bytes = new Uint8Array(n);
6471
crypt0.getRandomValues(bytes);

uuid-random.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)