Skip to content

Commit

Permalink
test: add benchmark
Browse files Browse the repository at this point in the history
Add a simple benchmark to be able to catch order-of-magnitude
performance regressions.
  • Loading branch information
ctavan committed Apr 30, 2020
1 parent a7d3d51 commit 4ea9774
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -363,6 +363,17 @@ import { v4 as uuidv4 } from 'uuid';
Workers](https://caniuse.com/#feat=cryptography) and we are not aware of a polyfill (let us know if
you find one, please).

## Developing

We happily accept pull requests!

If you do considerable changes please [run the benchmark](./examples/benchmark/) before and after
your changes and include the results in the pull request description:

```
npm run test:benchmark
```

## Upgrading From uuid\@7

### Only Named Exports Supported When Using with Node.js ESM
Expand Down
11 changes: 11 additions & 0 deletions README_js.md
Expand Up @@ -353,6 +353,17 @@ import { v4 as uuidv4 } from 'uuid';
Workers](https://caniuse.com/#feat=cryptography) and we are not aware of a polyfill (let us know if
you find one, please).

## Developing

We happily accept pull requests!

If you do considerable changes please [run the benchmark](./examples/benchmark/) before and after
your changes and include the results in the pull request description:

```
npm run test:benchmark
```

## Upgrading From uuid\@7

### Only Named Exports Supported When Using with Node.js ESM
Expand Down
22 changes: 22 additions & 0 deletions examples/benchmark/README.md
@@ -0,0 +1,22 @@
# uuid Benchmark

```
npm install
npm test
```

Example output (`uuid@8.0.0`, MacBook Pro (Retina, 13-inch, Early 2015), 3.1 GHz Dual-Core Intel Core i7):

```
uuidv1() x 1,329,774 ops/sec ±1.67% (87 runs sampled)
uuidv1() fill existing array x 3,568,608 ops/sec ±1.67% (89 runs sampled)
uuidv4() x 305,160 ops/sec ±1.86% (85 runs sampled)
uuidv4() fill existing array x 352,852 ops/sec ±1.92% (85 runs sampled)
uuidv3() DNS x 115,482 ops/sec ±1.55% (80 runs sampled)
uuidv3() URL x 103,127 ops/sec ±1.49% (81 runs sampled)
uuidv3() MY_NAMESPACE x 107,009 ops/sec ±1.59% (80 runs sampled)
uuidv5() DNS x 113,219 ops/sec ±1.66% (82 runs sampled)
uuidv5() URL x 100,618 ops/sec ±4.13% (81 runs sampled)
uuidv5() MY_NAMESPACE x 95,596 ops/sec ±4.24% (74 runs sampled)
Fastest is uuidv1() fill existing array
```
58 changes: 58 additions & 0 deletions examples/benchmark/benchmark.js
@@ -0,0 +1,58 @@
const Benchmark = require('benchmark');
const { v1: uuidv1, v4: uuidv4, v3: uuidv3, v5: uuidv5 } = require('uuid');

const MY_NAMESPACE = '55238d15-c926-4598-b49d-cf4e913ba13c';

const suite = new Benchmark.Suite();
suite
.add('uuidv1()', function () {
uuidv1();
})
.add('uuidv1() fill existing array', function () {
const array = new Array();
uuidv1(null, array, 0);
})
.add('uuidv4()', function () {
uuidv4();
})
.add('uuidv4() fill existing array', function () {
const array = new Array();
uuidv4(null, array, 0);
})
.add('uuidv3() DNS', function () {
uuidv3('hello.example.com', uuidv3.DNS);
})
.add('uuidv3() URL', function () {
// ... using predefined URL namespace (for, well, URLs)
uuidv3('http://example.com/hello', uuidv3.URL);
})
.add('uuidv3() MY_NAMESPACE', function () {
// ... using a custom namespace
//
// Note: Custom namespaces should be a UUID string specific to your application!
// E.g. the one here was generated using this modules `uuid` CLI.
uuidv3('Hello, World!', MY_NAMESPACE);
})
.add('uuidv5() DNS', function () {
// ... using predefined DNS namespace (for domain names)
uuidv5('hello.example.com', uuidv5.DNS);
})
.add('uuidv5() URL', function () {
// ... using predefined URL namespace (for, well, URLs)
uuidv5('http://example.com/hello', uuidv5.URL);
})
.add('uuidv5() MY_NAMESPACE', function () {
// ... using a custom namespace
//
// Note: Custom namespaces should be a UUID string specific to your application!
// E.g. the one here was generated using this modules `uuid` CLI.
// const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
uuidv5('Hello, World!', MY_NAMESPACE);
})
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run({ async: true });
33 changes: 33 additions & 0 deletions examples/benchmark/package-lock.json

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

14 changes: 14 additions & 0 deletions examples/benchmark/package.json
@@ -0,0 +1,14 @@
{
"name": "uuid-benchmark",
"version": "0.0.0",
"private": true,
"scripts": {
"test": "node benchmark.js"
},
"dependencies": {
"uuid": "file:../../.local"
},
"devDependencies": {
"benchmark": "^2.1.4"
}
}
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -81,6 +81,8 @@
"pretest:node": "npm run build",
"test:node": "npm-run-all --parallel examples:node:**",
"test:pack": "./scripts/testpack.sh",
"pretest:benchmark": "npm run build",
"test:benchmark": "cd examples/benchmark && npm install && npm test",
"prettier:check": "prettier --ignore-path .prettierignore --check '**/*.{js,jsx,json,md}'",
"prettier:fix": "prettier --ignore-path .prettierignore --write '**/*.{js,jsx,json,md}'",
"bundlewatch": "npm run pretest:browser && bundlewatch --config bundlewatch.config.json",
Expand Down

0 comments on commit 4ea9774

Please sign in to comment.