JSONPath-Plus / JSONPath

A fork of JSONPath from http://goessner.net/articles/JsonPath/
Other
958 stars 169 forks source link

perf: improve evaluation speed of conditional queries #175

Closed jacobroschen closed 2 years ago

jacobroschen commented 2 years ago

PR description

Use NodeJS' vm.Script class to cache the script that the context needs to be evaluated against. This provides large speed improvements (~50%) when a path contains conditional or JS logic.

To evaluate the performance improvements, I ran json-querying-performance-testing against a 16" 2019 MacBook Pro with an i9. Three runs were averaged together, with deltas calculated against main.

smallCityLots
┌────────────────────┬──────────┬──────────┬─────────────┐
│      (index)       │ shallow  │   deep   │ conditional │
├────────────────────┼──────────┼──────────┼─────────────┤
│   jsonpath-plus    │  0.4027  │  0.3462  │   0.2765    │
├────────────────────┼──────────┼──────────┼─────────────┤
│ jsonpath-plus (PR) │  0.4085  │  0.3501  │   0.1416    │
├────────────────────┼──────────┼──────────┼─────────────┤
│     Time Delta     │ +0.0058  │ +0.0139  │  -0.1349    │
├────────────────────┼──────────┼──────────┼─────────────┤
│     % Speedup      │ -1.4403% │ -1.1265% │  +48.788%   │
└────────────────────┴──────────┴──────────┴─────────────┘
mediumCityLots
┌────────────────────┬──────────┬──────────┬─────────────┐
│      (index)       │ shallow  │   deep   │ conditional │
├────────────────────┼──────────┼──────────┼─────────────┤
│   jsonpath-plus    │  0.7341  │  0.7493  │   0.5278    │
├────────────────────┼──────────┼──────────┼─────────────┤
│ jsonpath-plus (PR) │  0.7424  │  0.7605  │   0.2579    │
├────────────────────┼──────────┼──────────┼─────────────┤
│     Time Delta     │ +0.0083  │ +0.0112  │  -0.2699    │
├────────────────────┼──────────┼──────────┼─────────────┤
│     % Speedup      │ -1.1306% │ -1.4947% │  +51.137%   │
└────────────────────┴──────────┴──────────┴─────────────┘
largeCityLots
┌────────────────────┬──────────┬──────────┬─────────────┐
│      (index)       │ shallow  │   deep   │ conditional │
├────────────────────┼──────────┼──────────┼─────────────┤
│   jsonpath-plus    │  1.9973  │  2.0534  │   1.1102    │
├────────────────────┼──────────┼──────────┼─────────────┤
│ jsonpath-plus (PR) │  2.0169  │  2.0846  │   0.5036    │
├────────────────────┼──────────┼──────────┼─────────────┤
│     Time Delta     │ +0.0196  │ +0.0312  │  -0.6066    │
├────────────────────┼──────────┼──────────┼─────────────┤
│     % Speedup      │ -0.9813% │ -1.5194% │  +54.639%   │
└────────────────────┴──────────┴──────────┴─────────────┘

One interesting thing to note is there does appear to be a slight (<2%) performance regression on shallow and deep queries. I think given the purpose of this library (advanced JSON Path selectors, with full logic), that the slight performance regression is well worth the large (~50%) performance increase for conditional queries.

In a real world project, with a wide range of selector complexity, I saw ~24% speed improvement.

Checklist

brettz9 commented 2 years ago

@jacobroschen : Would you be interested in maintaining the library if @s3u approves? I no longer have the energy to do more than some trivial updates (and even that is uncertain).

jacobroschen commented 2 years ago

@brettz9 I'd be open to being a maintainer.

jacobroschen commented 2 years ago

@brettz9 It seems like @s3u also doesn't have time to review this. Any chance you could help move it along soon or make me a maintainer?

jacobroschen commented 2 years ago

Hi @brettz9, sorry to ping you again, but is there any chance we could make progress on this PR or add me as a maintainer? If not, that's fine and I can fork this repo and maintain that.

brettz9 commented 2 years ago

@jacobroschen : Apologies for the delay. I've added you as maintainer.

If you can just ensure that we keep up the tests and 100% coverage for any new features, maintaining backward compatibility and at least retaining performance as at all possible, that would be great. For new features, hopefully we can align with json-path-comparison, a json-path standardization effort.

json-querying-performance-testing might be of interest to you given your performance bent.

Best wishes and thanks very much!

jacobroschen commented 2 years ago

@brettz9 Thank you for you help here. I'll keep those notes in mind for future work, and I appreciate you getting this library to what it is today.

FredericKayser commented 2 years ago

Searched for a performance update, because at the moment it is causing problems with spectral. Tried spectral with this change, it improves the performance very well. Do you plan to release a patch version with this change in the near future?

Kind regards, Frederic

brettz9 commented 2 years ago

@jacobroschen : Let me know when ready for an npm release. @FredericKayser

jacobroschen commented 2 years ago

@brettz9 I'm good for an npm release. Is there anything you want me to do for it? Or will you handle it all?

brettz9 commented 2 years ago

@jacobroschen : For now, I can handle it; I'd like to keep the dev. packages up to date and just review. I've made it a minor release rather than patch release to be a little more careful with it not technically being a fix.