Hi all,
I'm importing the ajv module (for JSON schema validation) into NodeRED from npm to the GlobalContext as follows:
functionGlobalContext = {
ajv:require(/path/to/node_modules/ajv'),
and then using it in a function node:
const Ajv = global.get("ajv.default");
// JSON schema
const commandsSchema = {
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
...}
};
const ajvValidator = new Ajv();
var validate = ajvValidator.compile(commandsSchema);
// incoming msg.payload contains the JSON to validate
msg.result = validate(msg.payload);
return msg;
but the average latency to validate the payload is 3 - 4 seconds
there is an example here of a NodeRED node using Ajv for validation. It pulls the schema from a HTTP request and caches it locally, so presumably passing the schema to Ajv inline (as per the example above) would be just as fast - but the performance of Ajv when it's used from globalContext is much worse.
Could the difference in performance be that the ajv module loaded once on startup with the node-red-contrib-json-multi-schema node, but loaded every time a message is received when it's used from globalContext?
If so, any thoughts on how to improve the performance?
Thanks!