Skip to content

Commit

Permalink
perf: more optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
singuerinc committed May 31, 2018
1 parent dbe8929 commit 207ed92
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 35 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -74,6 +74,10 @@ randomNIEWith('X', 'E', 1); //=> 'X2080280E'

`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 build && yarn benchmark
```

### isValid

| lib | method | operations/sec | |
Expand All @@ -87,8 +91,8 @@ randomNIEWith('X', 'E', 1); //=> 'X2080280E'

| lib | method | operations/sec | |
| --------------------- | ---------- | -------------- | ----------- |
| better-dni | #ctrlChar | **9,328,614** | 2.3x faster |
| dni-js | #getLetter | 3,947,197 | |
| 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 | - | |

Expand Down
2 changes: 1 addition & 1 deletion benchmark.js
Expand Up @@ -96,7 +96,7 @@ benches.push(
})
);

Benchmark.invoke([benches[5]], {
Benchmark.invoke([...benches], {
name: 'run',
args: true,
queued: true
Expand Down
29 changes: 18 additions & 11 deletions dist/index.js

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

1 change: 1 addition & 0 deletions dist/index.js.map

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -40,6 +40,7 @@
"types": "types/index.d.ts",
"files": [
"dist/index.js",
"dist/index.js.map",
"types/index.d.ts"
]
}
1 change: 1 addition & 0 deletions rollup.config.js
Expand Up @@ -11,6 +11,7 @@ const banner =
'// Better DNI may be freely distributed under the MIT license.\n';

export default {
sourcemap: true,
input: 'src/index.js',
output: {
banner,
Expand Down
2 changes: 1 addition & 1 deletion src/ctrlChar.js
Expand Up @@ -12,6 +12,6 @@ import { _char } from './internal/_char';
* ctrlChar("03118880B"); // => 'B'
* ctrlChar("03118880"); // => 'B'
*/
const ctrlChar = x => _char(x.toUpperCase());
const ctrlChar = x => _char(x).toUpperCase();

export { ctrlChar };
8 changes: 6 additions & 2 deletions src/internal/_char.js
@@ -1,8 +1,12 @@
import { _letter } from './_utils';

const _char = y => {
const f = { X: '0', Y: '1', Z: '2' }[y[0]] || y[0];
const i = f + '' + y.substr(1, 7);
// Get a number from 0 - 2 when `y` is a NIE
let f = 'xyzXYZ'.indexOf(y[0]) % 3;
// Otherwise default to the number (NIF case only)
if (f === -1) f = y[0];
// Strip the letters
const i = `${f}${y.slice(1, 8)}`;
return _letter(i);
};

Expand Down
15 changes: 3 additions & 12 deletions src/internal/_utils.js
Expand Up @@ -9,17 +9,8 @@ _Random.prototype.next = function() {

// _Random :: https://gist.github.com/blixt/f17b47c62508be59987b#file-prng-js

const LETTERS = 'TRWAGMYFPDXBNJZSQVHLCKE';

const _idxOf = x => y => x.indexOf(y);
const _xyzAsNum = _idxOf('xyz');
const _lastIndex = _idxOf(LETTERS);
const _upper = x => x.toUpperCase();

const _letter = x => LETTERS[+x % 23];
const _randStrLimit = limit => ('' + Math.random()).substr(-limit);
const _letter = x => 'trwagmyfpdxbnjzsqvhlcke'[+x % 23];
const _randStrLimit = limit => `${Math.random()}`.slice(-limit);
const _randFloat = seed => (new _Random(seed).next() - 1) / 2147483646;

const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);

export { _letter, _randStrLimit, _Random, _lastIndex, _upper, _randFloat, _xyzAsNum };
export { _letter, _randStrLimit, _randFloat };
5 changes: 3 additions & 2 deletions src/isNIE.js
@@ -1,5 +1,4 @@
import { _isNIE } from './internal/_isNIE';
import { _upper } from './internal/_utils';
import { ctrlChar } from './ctrlChar';

/**
Expand All @@ -11,7 +10,9 @@ import { ctrlChar } from './ctrlChar';
* isNIE("X4108613P"); // => true
*/
const isNIE = value => {
return !!value && value.length === 9 && _isNIE(value) && ctrlChar(value) === _upper(value[8]);
return (
!!value && value.length === 9 && _isNIE(value) && ctrlChar(value) === value[8].toUpperCase()
);
};

export { isNIE };
5 changes: 3 additions & 2 deletions src/isNIF.js
@@ -1,5 +1,4 @@
import { _isNIF } from './internal/_isNIF';
import { _upper } from './internal/_utils';
import { ctrlChar } from './ctrlChar';

/**
Expand All @@ -11,7 +10,9 @@ import { ctrlChar } from './ctrlChar';
* isNIF("93375221M"); // => true
*/
const isNIF = value => {
return !!value && value.length === 9 && _isNIF(value) && ctrlChar(value) === _upper(value[8]);
return (
!!value && value.length === 9 && _isNIF(value) && ctrlChar(value) === value[8].toUpperCase()
);
};

export { isNIF };
3 changes: 2 additions & 1 deletion src/randomNIE.js
Expand Up @@ -10,7 +10,8 @@ import { _randStrLimit, _letter } from './internal/_utils';
const randomNIE = () => {
const r = Math.floor(Math.random() * 3);
const nn = _randStrLimit(7);
return ['X', 'Y', 'Z'][r] + nn + _letter(+(r + '' + nn));
const l = _letter(+`${r}${nn}`);
return `${'XYZ'[r]}${nn}${l}`;
};

export { randomNIE };
2 changes: 1 addition & 1 deletion src/randomNIEWith.js
@@ -1,4 +1,4 @@
import { _upper, _randFloat } from './internal/_utils';
import { _randFloat } from './internal/_utils';
import { ctrlChar } from './ctrlChar';

/**
Expand Down

0 comments on commit 207ed92

Please sign in to comment.