Skip to content

Commit

Permalink
Merge pull request #16 from jchook/15-ios-9
Browse files Browse the repository at this point in the history
Fixes iOS 9 issue and improves benchmarks
  • Loading branch information
jchook authored Jun 24, 2020
2 parents 42f6bf7 + 114951c commit 6e96a2c
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 60 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG → CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
The user-friendly summaries of how this project evolved over
time.

## 1.3.1 <small>- Jun 19, 2020</small>

- Fixes issue with iOS 9 ([#15](https://github.com/jchook/uuid-random/issues/15))
- Improves benchmarks


## 1.3.0 <small>- Sept 14, 2019</small>

- 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))
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ Compatible with almost all versions of:

## Performance

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.
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).

| npm package | performance |
|-----------------|-----------------|
| portable-uuid | 487k ops/sec |
| uuid | 502k ops/sec |
| id128 | 2.1M ops/sec |
| **uuid-random** <small>(this)</small> | **2.7M ops/sec** |
| portable-uuid | 354k ops/sec |
| uuid | 474k ops/sec |
| id128 | 6.0M ops/sec |
| **uuid-random** <small>(this)</small> | **9.7M ops/sec** |

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

## Why use UUID?

Expand Down
53 changes: 0 additions & 53 deletions benchmark.js

This file was deleted.

23 changes: 23 additions & 0 deletions benchmark/benchmark.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>
<head>
<script src="node_modules/lodash/lodash.js"></script>
<script src="node_modules/benchmark/benchmark.js"></script>
<script src="../index.js"></script>
</head>

<body>
<script>
var suite = new Benchmark.Suite()
console.log('Starting benchmark...')
suite
.add('uuid-random', uuid)
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })
</script>
</body>
</html>
28 changes: 28 additions & 0 deletions benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
*
* Benchmark using the `benchmark` module.
*
* Unfortunately this module somehow adds overhead to the function calls
* so the true ops/sec cannot be determined.
*
*/

var Benchmark = require('benchmark')
var suite = new Benchmark.Suite()
var id128 = require('id128')

suite
.add('uuid-random', require('..'))
.add('id128', function () { id128.Uuid4.generate().toCanonical() })
.add('portable-uuid', require('portable-uuid'))
.add('uuid', require('uuid').v4)
.add('nanoid', require('nanoid').nanoid)
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })


46 changes: 46 additions & 0 deletions benchmark/dispersion.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<html>
<body>
<script type="text/javascript">

const HEIGHT = 250;
const WIDTH = 250;
const ITERS = 250000;
const AREA = HEIGHT * WIDTH;

function cryptoRandom() {
// Divide a random UInt32 by the maximum value (2^32 -1) to get a result between 0 and 1
return window.crypto.getRandomValues(new Uint32Array(1))[0] / 4294967295;
}

function randomCoords(rnd) {
var num = Math.floor(rnd() * AREA);
var xx = Math.floor(num / WIDTH);
var yy = num % HEIGHT;
return [xx, yy];
}

function doRender(id, rnd) {
var h1 = document.createElement('h1');
h1.innerHTML = id;
document.body.appendChild(h1);
var canvas = document.createElement('canvas');
document.body.appendChild(canvas);
var ctx = canvas.getContext('2d');
canvas.setAttribute('height', HEIGHT);
canvas.setAttribute('width', WIDTH);

var xx, yy, ii;

for (var ii = 0; ii < ITERS; ii++) {

[xx, yy] = randomCoords(rnd);
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(xx, yy, 1, 1);
}
}

doRender('mathRandom', Math.random);
doRender('cryptoRandom', cryptoRandom);
</script>
</body>
</html>
18 changes: 18 additions & 0 deletions benchmark/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "uuid-random-benchmark",
"version": "1.0.0",
"description": "Benchmarking various uuid libraries",
"main": "index.js",
"author": "Wes Roberts",
"license": "MIT",
"dependencies": {
"benchmark": "^2.1.4",
"fast-stats": "^0.0.5",
"id128": "^1.5.0",
"microtime": "^3.0.0",
"nanoid": "^3.1.10",
"platform.js": "^1.0.0",
"portable-uuid": "^0.3.0",
"uuid": "^8.1.0"
}
}
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@
return crypt0.randomBytes;
}
if (crypt0.getRandomValues) {
if (typeof Uint8Array.prototype.slice !== 'function') {
return function(n) {
var bytes = new Uint8Array(n);
crypt0.getRandomValues(bytes);
return Array.from(bytes);
};
}
return function(n) {
var bytes = new Uint8Array(n);
crypt0.getRandomValues(bytes);
Expand Down
2 changes: 1 addition & 1 deletion uuid-random.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6e96a2c

Please sign in to comment.