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?
As a guess, it may be because it is having to recompile the schema for every message.
Did you know the built-in JSON node can do schema validation? If you set msg.schema to the schema you want it to use, it will validate the payload for you. If there is an error with the validation, I believe it will log an error that can be handled with the catch node. Some more information is available in the sidebar help for the node.