-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Joi.validate is slow, Joi.assert is slower #2340
Comments
ajv works completely different than joi. It builds custom validation functions in runtime which means it can produce code that basically does exactly what your vanilla example does. The only overhead is an extra function call. joi is never going to be as fast as ajv, unless we decide to rewrite the entire system for custom compiled functions which I doubt will ever happen. For super simple schemas, you are always going to pay an oversized price when using joi because it has a lot of setup per validation to support advanced features as well as composability. joi gives you significantly more validation power.
|
I've ran the same tests with a different benchmark tool and got the same results but with more useful output:
Yes, ajv is 100x faster than joi for a schema as simple as this, but at 8m operations a second, joi would be perfectly fine in most real world use cases. When working on v16 we've done a lot of work to make things faster and v16 is faster than v15. I am not planning on doing any more performance work on joi for the foreseeable future (that is, in 2020). At the end of the day, you need to pick the tool that is right for your application. If performance if your top priority and you need those fractional milliseconds, ajv is going to be a better fit at the cost of reduced functionality. |
I'll admit I kind of expected a full repro of the schema where you halved the response time, this benchmark doesn't really help as much as having a real life scenario. |
@Marsup We went from using joi.assert to vanilla. We make thousands of calls with simple schema validation (a bit more complex than boolean schema but not much). The difference was a few hundred ms. Plain node assert doesn't seem to carry the overhead that joi.assert carries. As for not using joi.assert in hot path.. Different use cases have different requirements. Who knew that it carried that much overhead. |
I know about slowness of assert and why that is, there might be something actionable there. So things are unclear for me right now, did you simply make the switch to validate or did you convert to json-schema? |
@Marsup We ultimately switched to vanilla since joi-to-json had some limitations (which are now resolved btw). However if I had know about the significant difference between assert and valdiate it would have been enough (and much simpler!) to just switch that. /shrug |
This ticket came from #2253 (comment) and was prompted by @Marsup
Using the following bench code:
I get the output
Asking the questions:
Joi.assert
so much slower thanJoi.validate
?ajv
so much faster thanJoi.validate
?The text was updated successfully, but these errors were encountered: