Blazing Fast Performance with Compilation
json-logic-engine
has support for logic-compilation which greatly enhances run-time performance of your logic. In a number of (simpler) cases, it can get rather close to native performance. Running many iterations of
json-logic-js
's test suite, we can observe stark performance differences between the built versions of the logic-engine against json-logic-js
. Some of the additional features of the engine do seem to cause the interpreted version of the engine to perform slightly slower. > node test.jsjson-logic-js: 10.872sle interpreted: 13.291sle built: 1.695sle async built: 4.171s
This comparison is not fair though, as the compilation mechanism is able to evaluate whether a particular branch is deterministic & pre-compute portions of the logic in advance. Running a modified test suite that can't be pre-computed unfairly yields this alternative set of results:
> node test.jsjson-logic-js: 741.06msle interpreted: 858.67msle built: 22.686msle async built: 75.483ms
Additionally, the compilation mechanism allows the asynchronous version of the engine to perform quite well against its interpreted counter-part.
> node perf.js & node perf2.jsinterpreted: 23.365sbuilt: 185.105ms
Comparing the engine against an alternative library like
json-rules-engine
, {any: [{all: [{fact: 'gameDuration',operator: 'equal',value: 40}, {fact: 'personalFoulCount',operator: 'greaterThanInclusive',value: 5}]}, {all: [{fact: 'gameDuration',operator: 'equal',value: 48}, {fact: 'personalFoulCount',operator: 'greaterThanInclusive',value: 6}]}]}
vs
{or: [{and: [{'===': [40, { var: 'gameDuration' }]}, {'>=': [{ var: 'personalFoulCount' }, 5]}]},{and: [{'===': [48, { var: 'gameDuration' }]}, {'>=': [{ var: 'personalFoulCount' }, 6]}]}]}
The performance difference is staggering:
> node rules.jsjson-logic-engine: 83.782msjson-rules-engine: 39.928s
To use this feature, you merely have to call:
const func = engine.build(logic)
And invoke
func
with the data you'd like to run it with.