Are there some low hanging fruits to improve performance? ow
seems to be about 15 times slower in validation than pure JS. I understand that some slowness is obviously expected, but 15 times slower makes me think this could be a bug rather than a design limitation.
plain js took 19.86ms on average.
ow took 294.62ms on average.
I'm using [email protected]
Repro code:
const ow = require('ow').default;
const objs = Array(1000).fill({
foo: 'bar',
one: 1,
obj: {},
})
function validateWithJs(objects) {
function validate(obj, schema) {
Object.entries(schema).forEach(([prop, validator]) => {
return validator(prop);
})
}
const schema = {
foo: (prop) => typeof prop === 'string',
one: (prop) => typeof prop === 'number',
obj: (prop) => prop && typeof prop === 'object',
};
objects.forEach(obj => {
validate(obj, schema)
})
}
function validateWithOw(objects) {
const predicate = ow.object.exactShape({
foo: ow.string,
one: ow.number,
obj: ow.object,
})
objects.forEach(obj => {
ow(obj, predicate)
})
}
function benchmark(name, fn) {
const millis = [];
const start = Date.now();
for (let i = 0; i < 50; i++) {
fn()
millis.push(Date.now() - start);
}
const total = millis.reduce((sum, num) => sum + num);
const avg = total / millis.length;
console.log(`${name} took ${avg}ms on average.`);
}
benchmark('plain js', () => validateWithJs(objs))
benchmark('ow', () => validateWithOw(objs))
It is a problem for us because we use ow
to validate user provided objects (constructor options) and the number of those objects can be hundreds of thousands in one batch. ow
is by far the slowest part of that process.
I'd gladly submit a PR if you can point me in some general direction.
Thanks.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too