APIDevTools / json-schema-ref-parser

Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers
https://apitools.dev/json-schema-ref-parser
MIT License
957 stars 228 forks source link

Performance issue #176

Closed Eli-Black-Work closed 3 years ago

Eli-Black-Work commented 4 years ago

First of all, thanks for writing this library :)

We're running into a performance issue that I wanted to alert you to.

We're loading a fairly large (~900KB) JSON schema file, parsing it, and running the resulting JavaScript object through this library's dereference() function. The dereference() call takes around 68 seconds to complete.

We've disabled all loading of local or external files, so I don't think that any of the time spent in dereference() is due to loading external resources.

Here's basically what the code looks like:

const rawSchema = fetch(...);
const schema = JSON.parse(rawSchema);

if (typeof (schema) !== 'object')
    throw `The schema was not an object.`;

const options = {
    parse: {
        json: false,
        yaml: false,
        text: false,
        binary: false
    },
    resolve: {
        external: false,
        file: false,
        http: false
    },
    continueOnError: false,
    dereference: {
        circular: true
    }
};

console.log(performance.now());

$SchemaRefParser
    .dereference(schema, options)
    .then(dereferencedSchema => {
        console.log(performance.now());
    });

The schema does contain circular references.

tahpot commented 4 years ago

I have similar issues with a 4MB json schema file (FHIR).

paztis commented 4 years ago

any updates for this defect ? I'm also facing it

paztis commented 4 years ago

I encounter this performance problem in case of non direct circular reference

In this simple case, I enter 680 times in the dereference$Ref function

"interface-.ts-69348-69886-.ts-0-209362": {
      "type": "object",
      "properties": {
        "byteLength": {
          "type": "number"
        },
        "slice": {
          "type": "function",
          "arguments": {
            "begin": {
              "type": "number"
            },
            "end": {
              "type": "number"
            }
          },
          "return": {
            "$ref": "#/definitions/interface-.ts-69348-69886-.ts-0-209362"
          },
          "required": [
            "begin"
          ]
        }
      },
      "required": [
        "byteLength",
        "slice"
      ],
      "additionalProperties": false
    },

Can you take a look at this problem ? this is really annoying

paztis commented 4 years ago

Sorry bad example, I didn't pass so much time inside this one. Try to find a better example

paztis commented 4 years ago

found a fix It's available in my PR: https://github.com/APIDevTools/json-schema-ref-parser/pull/195 Now performances are really good