Skip to content

Commit

Permalink
perf: faster with variable dni #wip
Browse files Browse the repository at this point in the history
  • Loading branch information
singuerinc committed May 22, 2018
1 parent 06dcbb6 commit 17c0817
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 59 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ yarn add better-dni
import { isValid, isNIE, isNIF } from 'better-dni';

isValid('Z7662566Y'); // => true
isValid('Z7662566-Y'); // => true

isNIE('x0000000a'); // => true
isNIF('00000000A'); // => true

// it also handle some common user input mistakes and special cases
isValid(' Z7662566Y '); // => true
isValid('Z7662566-Y'); // => true
```

## Benchmark
Expand All @@ -36,10 +34,10 @@ isValid('Z7662566-Y'); // => true

| lib | method | operations/sec |
| --------------------- | ----------- | -------------- |
| better-dni | isValid | **71,934,704** |
| dni-js | isValid | 2,630,463 |
| dni-js-validator | isValid | 6,455,491 |
| @willowi/validate-nif | validateNif | 755,383 |
| better-dni | isValid | **5,077,942** |
| dni-js-validator | isValid | 2,700,560 |
| dni-js | isValid | 2,596,587 |
| @willowi/validate-nif | validateNif | 640,322 |

> Benchmark on a MacBook Pro (Retina, 13-inch, Early 2015) - 3,1 GHz Intel Core i7 - 16 GB 1867 MHz DDR3
Expand Down
42 changes: 3 additions & 39 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,18 @@ const { isValid: dni_js_isValid } = require('dni-js');
const { isValid: dni_js_validator_isValid } = require('dni-js-validator');
const { validateNif } = require('@willowi/validate-nif');

// console.log(better_dni_isValid('12345678Z'));

// console.log('better_dni_isValid', better_dni_isValid('12345678Z'));
// console.log('dni_js_isValid', dni_js_isValid('12345678Z'));
// console.log('dni_js_validator_isValid', dni_js_validator_isValid('12345678Z'));
// console.log('validateNif', validateNif('12345678Z'));

const suite1 = new Benchmark.Suite();

suite1
.add('better-dni#isValid', function() {
better_dni_isValid('12345678Z');
})
.add('dni-js#isValid', function() {
dni_js_isValid('12345678Z');
})
.add('dni-js-validator#isValid', function() {
dni_js_validator_isValid('12345678Z');
})
.add('dni-js#isValid', function() {
dni_js_isValid('12345678Z');
})
.add('@willowi/validate-nif#validateNif', function() {
validateNif('12345678Z');
})
Expand All @@ -33,32 +26,3 @@ suite1
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run({ async: true });

// const suite2 = new Benchmark.Suite();
// let i = 0;
// const count = i => i % 4;
// const randomNIF = () => {
// i++;
// return ['12345678Z', 'X9464187D', 'X9464186P', '03118880B'][count(i)];
// };

// suite2
// .add('better-dni#isValid with variable nif', function() {
// better_dni_isValid(randomNIF());
// })
// .add('dni-js#isValid with variable nif', function() {
// dni_js_isValid(randomNIF());
// })
// .add('dni-js-validator#isValid with variable nif', function() {
// dni_js_validator_isValid(randomNIF());
// })
// .add('@willowi/validate-nif#validateNif with variable nif', function() {
// validateNif(randomNIF());
// })
// .on('cycle', function(event) {
// console.log(String(event.target));
// })
// .on('complete', function() {
// console.log('Fastest is ' + this.filter('fastest').map('name'));
// })
// .run({ async: true });
20 changes: 10 additions & 10 deletions dni.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ const { sanitize, isValid, isNIE, isNIF } = require('./index');

describe('dni', () => {
describe('sanitize', () => {
it('should capitalize', () => {
assert.equal('X0000000A', sanitize('x0000000a'));
it('should convert to lowercase', () => {
assert.equal('x0000000a', sanitize('x0000000a'));
});

it('should remove white spaces', () => {
assert.equal('X0000000A', sanitize('X0 0000 00 A'));
it.skip('should remove white spaces', () => {
assert.equal('x0000000a', sanitize('X0 0000 00 A'));
});

it('should remove underscores', () => {
assert.equal('X0000000A', sanitize('X0_0000_00_A'));
it.skip('should remove underscores', () => {
assert.equal('x0000000a', sanitize('X0_0000_00_A'));
});

it('should remove dashes', () => {
assert.equal('X0000000A', sanitize('X0-0000-00-A'));
it.skip('should remove dashes', () => {
assert.equal('x0000000a', sanitize('X0-0000-00-A'));
});
});

Expand Down Expand Up @@ -76,11 +76,11 @@ describe('dni', () => {
assert.equal(isValid('z7662566y'), true);
});

it('should validate " Z7662566-Y"', () => {
it.skip('should validate " Z7662566-Y"', () => {
assert.equal(isValid(' z7662566-y'), true);
});

it('should validate " Z7662566-Y "', () => {
it.skip('should validate " Z7662566-Y "', () => {
assert.equal(isValid(' z7662566-y '), true);
});

Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ const isNIF = value => {
* @returns {boolean}
*/
const isValid = value => {
const dni = sanitize(value); //.replace(/\s_-/, '');
const dni = (!!value ? value : '').toLowerCase();

if (!_isNIF(dni) && !_isNIE(dni)) return false;
if (!value && !_isNIF(dni) && !_isNIE(dni)) return false;

const f = { x: 0, y: 1, z: 2 }[dni[0]] || dni[0];
const dni_1_to_7 = dni.substr(1, 7);
Expand Down

0 comments on commit 17c0817

Please sign in to comment.