Last active
February 9, 2016 13:25
-
-
Save Dunkelheit/0553121c923c2a47f128 to your computer and use it in GitHub Desktop.
Benchmark between schema-validator and tv4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Benchmark = require('benchmark'); | |
var schemaManager = require('schema-inspector'); | |
var tv4 = require('tv4'); | |
var suite = new Benchmark.Suite; | |
var schema = { | |
count: { | |
optional: true | |
}, | |
offset: { | |
optional: true | |
}, | |
q: { | |
type: 'string', | |
optional: true | |
}, | |
sort: { | |
type: 'string', | |
optional: true | |
}, | |
order: { | |
type: 'string', | |
optional: true, | |
eq: ['asc', 'desc'] | |
}, | |
filterId: { | |
optional: true | |
}, | |
promotionId: { | |
optional: true | |
}, | |
categoryId: { | |
optional: true | |
}, | |
tag: { | |
optional: true | |
}, | |
id: { | |
optional: true | |
} | |
}; | |
var sanitizer = { | |
count: { | |
type: 'integer', | |
optional: true | |
}, | |
offset: { | |
type: 'integer', | |
optional: true | |
} | |
}; | |
var jsonSchema = { | |
type: 'object', | |
required: ['q', 'count', 'offset'], // Made different on purpose to put some extra "load" in json-schema | |
properties: { | |
count: { | |
type: 'string' | |
}, | |
offset: { | |
type: 'string' | |
}, | |
q: { | |
type: 'string' | |
}, | |
sort: { | |
type: 'string' | |
}, | |
order: { | |
type: 'string' | |
}, | |
filterId: { | |
type: 'string' | |
}, | |
promotionId: { | |
type: 'string' | |
}, | |
categoryId: { | |
type: 'string' | |
}, | |
tag: { | |
type: 'string' | |
}, | |
id: { | |
type: 'string' | |
} | |
} | |
}; | |
suite.add('schema-inspector#validate', function () { | |
var request = { | |
q: 'kip', | |
count: 10, | |
offset: 0 | |
}; | |
schemaManager.validate({ type: 'object', properties: schema }, request); | |
}).add('schema-inspector#sanitizeAndValidate', function () { | |
var request = { | |
q: 'kip', | |
count: 10, | |
offset: 0 | |
}; | |
schemaManager.sanitize({ type: 'object', properties: sanitizer }, request); | |
schemaManager.validate({ type: 'object', properties: schema }, request); | |
}).add('TV4#validate', function () { | |
var params = { | |
q: 'kip', | |
count: '10', | |
offset: '0' | |
}; | |
tv4.validate(params, jsonSchema); | |
params.count = parseInt(params.count, 10); | |
params.offset = parseInt(params.offset, 10); | |
}).add('TV4#validateResult', function () { | |
var params = { | |
q: 'kip', | |
count: '10', | |
offset: '0' | |
}; | |
var result = tv4.validateResult(params, jsonSchema); | |
params.count = parseInt(params.count, 10); | |
params.offset = parseInt(params.offset, 10); | |
}).add('TV4#validateMultipleSuccessful', function () { | |
var params = { | |
q: 'kip', | |
count: '10', | |
offset: '0' | |
}; | |
var result = tv4.validateMultiple(params, jsonSchema); | |
params.count = parseInt(params.count, 10); | |
params.offset = parseInt(params.offset, 10); | |
}).add('TV4#validateMultipleFailed', function () { | |
var params = { | |
count: '10', | |
offset: '0' | |
}; | |
var result = tv4.validateMultiple(params, jsonSchema); // Fails | |
params.count = parseInt(params.count, 10); | |
params.offset = parseInt(params.offset, 10); | |
}).on('cycle', function (event) { | |
console.log(String(event.target)); | |
}).on('complete', function () { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}).run({'async': true}); |
Author
Dunkelheit
commented
Feb 9, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment