Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router: use fast-json-stringify to improve the performance #97

Closed
bajtos opened this issue Mar 29, 2017 · 5 comments
Closed

Router: use fast-json-stringify to improve the performance #97

bajtos opened this issue Mar 29, 2017 · 5 comments

Comments

@bajtos
Copy link
Member

bajtos commented Mar 29, 2017

See https://github.com/fastify/fastify/blob/master/docs/Validation-And-Serialize.md#serialize, https://www.npmjs.com/package/fast-json-stringify, consider also https://www.npmjs.com/package/fast-safe-stringify.

The fastify framework claims it's gaining 1-4x speed-up from using this module.

Since our routes (controller methods) provide swagger metadata describing the expected output format in a JSON-Schema-like format, we should be able to leverage this metadata to improve the performance.

@raymondfeng
Copy link
Contributor

@bajtos
Copy link
Member Author

bajtos commented Apr 12, 2017

@raymondfeng

AFAICT from JSONStream's README, it's solving a different problem.

In LoopBack, we have a single object/array returned by a remote method (a Controller method) that we want to write to the HTTP response stream.

JSONStream allows users to write the JSON array-items/object-properties via a streaming interface. I don't see how this is going to improve our performance in the case where we already have the full response object available?

It would be useful if our ORM/DAO implementation provided methods returning a stream of array items (think of MyModel.find returning a stream instead of an array, or already-implemented KeyValueModel.iterateKeys returning an async iterator). However, that is out of scope of this issue. I opened a new story to track this idea - see #122

@ritch
Copy link
Contributor

ritch commented Apr 12, 2017

AFAICT from JSONStream's README, it's solving a different problem.

This is totally different. The performance increase is similar to ProtoBuffs minus the binary (footprint) benefits.

I'm guessing this approach uses the fact that the JSONSchema acts as essentially a template that is rendered by the serializer instead of JSON.stringify() which has to walk a potentially infinitely complex tree.

Rendering the template is much simpler than walking the tree since the template basically gives the serializer a map of where to go.

@raymondfeng
Copy link
Contributor

I'm also concerned about the requirement of a schema. The API input/output tends to be flexible and extensible.

The JSON parse/stringify is not only CPU intensive, but also memory heavy. We see issues at both incoming http req/res and outgoing REST calls to other services. It often involves stream --> string --> object or vice versa. I think JSONStream can partially remove the need of string phase.

@bajtos
Copy link
Member Author

bajtos commented Dec 15, 2017

I am closing this issue because we are not going to have time to work on this in the next 6-9 months. However, we are happy to accept a pull request contributing this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants