Express State v1.1.0 — Performance
Certain apps pump a lot of data from the server to the client via Express State. This release greatly improves performance of serializing unchanging data with the new {cache: true}
option for the app.expose()
and res.expose()
methods.
Increasing Performance
Lazy serialization of exposed data is a primary feature of Express State. Exposed data objects are stored by reference, making them "live" and allowing their values to be updated even after the object has been passed to the expose()
method. And this is great for data which is dynamic per-request. But when data is unchanging/static, this means Express State runs through its serialization process for every request yielding the same result.
To avoid having Express State perform its serialization process for the same, static, data object, the new {cache: true}
option can be used:
var CONFIG = {
hostname : 'example.com',
someOther: 'constant value'
};
app.expose(CONFIG, 'MY_APP.config', {cache: true});
Setting this option allows Express State to optimize the serialization process
by keeping the serialized value around and re-using it every time the
toString()
method is invoked (which happens for every request.)
Benchmark Results
As part of the development of the new {cache: true}
option, benchmark tests have been added Express State; these include real-world fixture data from Photos Near Me and Yahoo Tech.
Serializing a simple object:
simple.toString() x 220,056 ops/sec ±3.56% (84 runs sampled)
simpleCached.toString() x 1,972,394 ops/sec ±4.58% (85 runs sampled)
Photos Near Me's static app-scoped data:
pnmApp.toString() x 15,840 ops/sec ±5.44% (84 runs sampled)
pnmAppCached.toString() x 241,270 ops/sec ±4.92% (83 runs sampled)
Yahoo Tech's static app-scoped data:
ytApp.toString() x 9,509 ops/sec ±6.08% (85 runs sampled)
ytAppCached.toString() x 339,479 ops/sec ±5.94% (71 runs sampled)
When data can be eagerly serialized by Express State, like static app-level configuration, the result of using the {cache: true}
option can improve repeated, per-request serialization by: 8x – 36x!
With these kinds of performance gains and when a large amount of data needs to be exposed to the client-side, it is recommended to come up with a strategy where all data which is common to most/every request be exposed at the app-scope with the {cache: true}
option set.
Deprecations
In order to support the new cache
option, the expose( obj, namespace, local )
API signature has been deprecated. The third argument is now options
, use: expose( obj, namespace, {local: local})
. The deprecated signature will be removed in a future release, and logs a warning when it is used.