-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for lazy JSONPath queries.
- Loading branch information
Showing
18 changed files
with
402 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ benchmark | |
tests/browser | ||
docs | ||
tests/path/cts | ||
performance/index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,33 @@ Sally @ $['users'][2]['name'] | |
Jane @ $['users'][3]['name'] | ||
``` | ||
|
||
### Lazy queries | ||
|
||
[`lazyQuery()`](./api/namespaces/jsonpath.md#lazyquery) is an alternative to `query()`. `lazyQuery()` can be faster and more memory efficient if querying large datasets, especially when using recursive descent selectors. Conversely, `query()` is usually the better choice when working with small datasets. | ||
|
||
`lazyQuery()` returns an iterable sequence of [`JSONPathNode`](./api/classes/jsonpath.JSONPathNode.md) objects which is not a `JSONPathNodeList`. | ||
|
||
```javascript | ||
import { lazyQuery } from "json-p3"; | ||
|
||
const data = { | ||
users: [ | ||
{ name: "Sue", score: 100 }, | ||
{ name: "John", score: 86 }, | ||
{ name: "Sally", score: 84 }, | ||
{ name: "Jane", score: 55 }, | ||
], | ||
}; | ||
|
||
for (const node of lazyQuery("$.users[[email protected] < 100].name", data)) { | ||
console.log(node.value); | ||
} | ||
|
||
// John | ||
// Sally | ||
// Jane | ||
``` | ||
|
||
### Compilation | ||
|
||
`query()` is a convenience function equivalent to `new JSONPathEnvironment().compile(path).query(data)`. Use `jsonpath.compile()` to construct a [`JSONPath`](./api/classes/jsonpath.JSONPath.md) object that can be applied to different data repeatedly. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* This is incomplete and, thus far, has been used in an adhoc manner. | ||
*/ | ||
const { performance } = require("perf_hooks"); | ||
const { jsonpath } = require("../dist/json-p3.cjs"); | ||
const cts = require("../tests/path/cts/cts.json"); | ||
|
||
function validQueries() { | ||
return cts.tests | ||
.filter((testCase) => testCase.invalid_selector !== true) | ||
.map((testCase) => { | ||
return [testCase.selector, testCase.document]; | ||
}); | ||
} | ||
|
||
function perf(repeat) { | ||
const env = new jsonpath.JSONPathEnvironment(); | ||
const queries = validQueries(); | ||
console.log( | ||
`repeating ${queries.length} queries on small datasets ${repeat} times`, | ||
); | ||
const start = performance.now(); | ||
for (let i = 0; i < repeat; i++) { | ||
for (const [query, data] of queries) { | ||
env.query(query, data); | ||
// Array.from(env.lazyQuery(query, data)); | ||
} | ||
} | ||
const stop = performance.now(); | ||
return (stop - start) / 1e3; | ||
} | ||
|
||
console.log(perf(1000)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ export { | |
Token, | ||
TokenKind, | ||
Nothing, | ||
lazyQuery, | ||
query, | ||
compile, | ||
} from "./path"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.