Skip to content

Commit

Permalink
fix: benchmark and playground
Browse files Browse the repository at this point in the history
  • Loading branch information
singuerinc committed May 29, 2021
1 parent 0c5699d commit 7fd7344
Show file tree
Hide file tree
Showing 19 changed files with 28,736 additions and 2,060 deletions.
4 changes: 4 additions & 0 deletions packages/better-dni/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
coverage
build
7 changes: 7 additions & 0 deletions packages/better-dni/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// eslint-disable-next-line no-undef
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
};
36 changes: 20 additions & 16 deletions packages/better-dni/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

![Better DNI](logo.png)

The fastest Spanish DNI (NIE / NIF) validation out there.
The fastest Spanish DNI (NIE / NIF) validation out there — 660 bytes.

## Installation

Expand Down Expand Up @@ -84,29 +84,33 @@ normalize(" x-9464186_p "); // => "X9464186P"

`better-dni` does a similar job as other libraries like [dni-js](https://github.com/albertfdp/dni-js/), [dni-js-validator](https://github.com/idirouhab/dni-js-validator), and [@willowi/validate-nif](https://github.com/WillowiDev/validate-nif) but `better-dni` is built with optimization and speed in mind. Take a look at these benchmark results:

```js
yarn && yarn build && yarn benchmark
```bash
yarn && yarn benchmark
```

### isValid

| lib | method | operations/sec | |
| --------------------- | ------------ | -------------- | ----------- |
| better-dni | #isValid | **10,128,148** | 3.5x faster |
| dni-js-validator | #isValid | 2,870,822 | |
| dni-js | #isValid | 2,448,090 | |
| @willowi/validate-nif | #validateNif | 683,573 | |
x 10.000.000 operations

| lib | method | time in ms | diff |
| --------------------- | ------------ | ---------- | ------------- |
| better-dni | #isValid | 737 ms | |
| dni-js-validator | #isValid | 2380 ms | x3.22 slower |
| dni-js | #isValid | 2610 ms | x3.54 slower |
| @willowi/validate-nif | #validateNif | 11000 ms | x14.92 slower |

### ctrlChar / getLetter

| lib | method | operations/sec | |
| --------------------- | ---------- | -------------- | ----------- |
| better-dni | #ctrlChar | **10,874,568** | 5.3x faster |
| dni-js | #getLetter | 2,032,845 | |
| dni-js-validator | no method | - | |
| @willowi/validate-nif | no method | - | |
x 10.000.000 operations

| lib | method | time in ms | diff |
| --------------------- | ---------- | ---------- | ------------ |
| better-dni | #ctrlChar | 791 ms | |
| dni-js | #getLetter | 2120 ms | x2.68 slower |
| dni-js-validator | no method | - | |
| @willowi/validate-nif | no method | - | |

> Benchmark on a MacBook Pro (Retina, 13-inch, Early 2015) - 3,1 GHz Intel Core i7 - 16 GB 1867 MHz DDR3 / Node v10.2.1
> Benchmark on a MacBook Pro (16-inch, 2019) - 2,6 GHz 6-Core Intel Core i7 - 16 GB 2667 MHz DDR4 / Node v14.15.1
## Demo

Expand Down
103 changes: 0 additions & 103 deletions packages/better-dni/benchmark.js

This file was deleted.

135 changes: 135 additions & 0 deletions packages/better-dni/benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { validateNif } from "@willowi/validate-nif";
import { getControlDigit, isValid as dniJsIsValid } from "dni-js";
import { isValid as dniJsValidatorIsValid } from "dni-js-validator";
import * as nanobench from "nanobench";
import { ctrlChar } from "./src/ctrlChar";
import { isNIE } from "./src/isNIE";
import { isNIF } from "./src/isNIF";
import { isValid } from "./src/isValid";
import { randomNIE } from "./src/randomNIE";
import { randomNIEWith } from "./src/randomNIEWith";
import { randomNIF } from "./src/randomNIF";
import { randomNIFWith } from "./src/randomNIFWith";

nanobench("isValid 10.000.000 times", function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
isValid("12345678Z");
}

b.end();
});

nanobench("dni-js-validator isValid 10.000.000 times", function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
dniJsValidatorIsValid("12345678Z");
}

b.end();
});

nanobench("dni-js-validator isValid 10.000.000 times", function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
dniJsIsValid("12345678Z");
}

b.end();
});

nanobench(
"@willowi/validate-nif validateNif 10.000.000 times",
function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
validateNif("12345678Z");
}

b.end();
}
);

nanobench("ctrlChar 10.000.000 times", function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
ctrlChar("X0729124R");
}

b.end();
});

nanobench("getControlDigit 10.000.000 times", function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
getControlDigit("X0729124R");
}

b.end();
});

nanobench.skip("randomNIE 10.000.000 times", function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
randomNIE();
}

b.end();
});

nanobench.skip("randomNIF 10.000.000 times", function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
randomNIF();
}

b.end();
});

nanobench("isNIE 10.000.000 times", function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
isNIE("Z9407822E");
}

b.end();
});

nanobench("isNIF 10.000.000 times", function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
isNIF("42406984K");
}

b.end();
});

nanobench("randomNIEWith 10.000.000 times", function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
randomNIEWith("Z", "E");
}

b.end();
});

nanobench("randomNIFWith 10.000.000 times", function (b: any) {
b.start();

for (let i = 0; i < 10_000_000; i++) {
randomNIFWith("E");
}

b.end();
});
Loading

0 comments on commit 7fd7344

Please sign in to comment.