Skip to content

Instantly share code, notes, and snippets.

@Dunkelheit
Last active February 9, 2016 13:25
Show Gist options
  • Save Dunkelheit/0553121c923c2a47f128 to your computer and use it in GitHub Desktop.
Save Dunkelheit/0553121c923c2a47f128 to your computer and use it in GitHub Desktop.
Benchmark between schema-validator and tv4
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});
@Dunkelheit
Copy link
Author

> node benchmark.js
schema-inspector#validate x 14,631 ops/sec ±6.96% (67 runs sampled)
schema-inspector#sanitizeAndValidate x 12,249 ops/sec ±2.30% (78 runs sampled)
TV4#validate x 215,749 ops/sec ±1.86% (73 runs sampled)
TV4#validateResult x 215,632 ops/sec ±1.96% (76 runs sampled)
TV4#validateMultipleSuccessful x 193,383 ops/sec ±2.19% (81 runs sampled)
TV4#validateMultipleFailed x 9,569 ops/sec ±1.97% (80 runs sampled) // WATCH OUT!
Fastest is TV4#validate,TV4#validateResult

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment