# Latency on JSON schema validation with ajv module

**URL:** <https://discourse.nodered.org/t/latency-on-json-schema-validation-with-ajv-module/41289>\
**Category:** General\
**Created:** [19 February 2021 10:29 UTC](https://discourse.nodered.org/t/latency-on-json-schema-validation-with-ajv-module/41289 "2021-02-19T10:29:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![episensor](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@episensor](https://discourse.nodered.org/u/episensor)\
**Post date:** [19 February 2021 10:29 UTC](https://discourse.nodered.org/t/latency-on-json-schema-validation-with-ajv-module/41289/1 "2021-02-19T10:29:53Z")

</div>

Hi all,

I'm importing the ajv module (for JSON schema validation) into NodeRED from npm to the GlobalContext as follows:

```auto
functionGlobalContext = { 
    ajv:require(/path/to/node_modules/ajv'),

```

and then using it in a function node:

```auto
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.

> <https://github.com/alexandrainst/node-red-contrib-json-multi-schema/blob/master/json-multi-schema-validator.js>

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!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [19 February 2021 10:39 UTC](https://discourse.nodered.org/t/latency-on-json-schema-validation-with-ajv-module/41289/2 "2021-02-19T10:39:19Z")

</div>

Hi @episensor

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.

---

<div class="post-metadata">

**Author:** ![episensor](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@episensor](https://discourse.nodered.org/u/episensor)\
**Post date:** [19 February 2021 11:16 UTC](https://discourse.nodered.org/t/latency-on-json-schema-validation-with-ajv-module/41289/3 "2021-02-19T11:16:27Z")

</div>

Ah! didn't realise the JSON node could do schema validation. 6ms latency now 😉 Thanks Nick!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 March 2021 11:17 UTC](https://discourse.nodered.org/t/latency-on-json-schema-validation-with-ajv-module/41289/4 "2021-03-05T11:17:09Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
