gbv / jskos-validate

Validation for JSKOS data.
MIT License
0 stars 1 forks source link

Importing jskos-validate is slow #15

Open nichtich opened 2 years ago

nichtich commented 2 years ago

Adding import validate from "jskos-validate" to your code will introduce a delay of half a second (depending on machine), can this be speed up? I guess it's the for-loop in lib/validate.js to sequentially compile JSON Schemas with ajv. Doing it in parallel with promises requires to switch jskos-validate to ES-modules.

stefandesu commented 2 years ago

Yeah, that's an issue. It should be done in parallel AND asynchronously. I don't see why we would need to switch to ES modules for working with promises though. The issue is rather that calling the validation method needs to wait for the JSON Schemas, but for that validate needs to become an async method which would break every single application.

I don't see a way to synchronously wait for the JSON Schema without busy-waiting, i.e. checking over and over again until the Schemas are available. Definitely not a good practice.

Another alternative would be to require programs to call and wait for an async method (that loads the JSON Schemas) before being able to use the (synchronous) validate methods. This would also break all applications that use this.

nichtich commented 2 years ago

We could generate pre-compile schemas and distribute as standalone validation code.

stefandesu commented 2 years ago

Yes, we should do that! Didn't realize it was an option. It will require a build step though that we don't currently have. Ideally, I would like to use the same system that we're using in cocoda-sdk and login-client (esbuild), but that would indeed require a rewrite to ES modules syntax.

How urgent is this? Is there a particular project where you noticed the slow startup due to jskos-validate?