openapi-contrib / openapi-schema-to-json-schema

Due to the OpenAPI v3.0 and JSON Schema discrepancy, you can use this JS library to convert OpenAPI Schema objects to proper JSON Schema.
https://www.npmjs.com/package/@openapi-contrib/openapi-schema-to-json-schema
MIT License
242 stars 20 forks source link

perf: do not deep clone upfront #14

Closed P0lip closed 4 years ago

P0lip commented 4 years ago

cloneDeep is fairly expensive in certain cases and does not need to be implement in the way it is now, since we visit each object, and hence can simply shallow copy it before modyfing. Using shallow copy of each object before visiting it significantly (by around 25-35%) improves the perfomance of @stoplight/http-spec#transformOas3Operations. Tested against various specs, such as Stripe, Whatsapp, etc. Obviously, the results will vary - if a given document does not have schemas or has a handful of small schemas, the difference is likely to be less notable.

P0lip commented 4 years ago

@philsturgeon @XVincentX any of you eager to review this one?

P0lip commented 4 years ago

Can you do one example of a clone deep that is not required for our usages? I can't really think of one at the moment

Technically we're doing deep cloning here - we copy each object we visit before mutating it. The PR title as well as the description might be a bit slightly misleading. What we're doing here is performing a shallow copy of the object we visit. This has a number of benefits:

If the sole aim is to avoid clone deep, maybe we can use lodash.clone? That'd make me feel more comfortable instead of our own implementation.

I'm strongly against using any of lodash clones in this particular case. They are great and take care of different object types such as Date, Maps, Sets, etc., but we do not need to clone any of them.

P0lip commented 4 years ago

@philsturgeon could you merge this one assuming you are fine with the changes?

github-actions[bot] commented 4 years ago

:tada: This PR is included in version 3.0.4 :tada:

The release is available on:

Your semantic-release bot :package::rocket: