Skip to content

perf: optimize walk method by 10-34% #179

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

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 24 additions & 33 deletions src/jsonpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,33 +352,28 @@ JSONPath.prototype._trace = function (
hasArrExpr));
// eslint-disable-next-line unicorn/prefer-switch -- Part of larger `if`
} else if (loc === '*') { // all child properties
this._walk(
loc, x, val, path, parent, parentPropName, callback,
(m, l, _x, v, p, par, pr, cb) => {
addRet(this._trace(unshift(m, _x), v, p, par, pr, cb,
true, true));
}
);
this._walk(val, (m) => {
addRet(this._trace(
x, val[m], push(path, m), val, m, callback, true, true
));
});
} else if (loc === '..') { // all descendent parent properties
// Check remaining expression with val's immediate children
addRet(
this._trace(x, val, path, parent, parentPropName, callback,
hasArrExpr)
);
this._walk(
loc, x, val, path, parent, parentPropName, callback,
(m, l, _x, v, p, par, pr, cb) => {
// We don't join m and x here because we only want parents,
// not scalar values
if (typeof v[m] === 'object') {
// Keep going with recursive descent on val's
// object children
addRet(this._trace(
unshift(l, _x), v[m], push(p, m), v, m, cb, true
));
}
this._walk(val, (m) => {
// We don't join m and x here because we only want parents,
// not scalar values
if (typeof val[m] === 'object') {
// Keep going with recursive descent on val's
// object children
addRet(this._trace(
expr.slice(), val[m], push(path, m), val, m, callback, true
));
}
);
});
// The parent sel computation is handled in the frame above using the
// ancestor object of val
} else if (loc === '^') {
Expand Down Expand Up @@ -408,15 +403,13 @@ JSONPath.prototype._trace = function (
if (this.currPreventEval) {
throw new Error('Eval [?(expr)] prevented in JSONPath expression.');
}
this._walk(
loc, x, val, path, parent, parentPropName, callback,
(m, l, _x, v, p, par, pr, cb) => {
if (this._eval(l.replace(/^\?\((.*?)\)$/u, '$1'), v[m], m, p, par, pr)) {
addRet(this._trace(unshift(m, _x), v, p, par, pr, cb,
true));
}
const safeLoc = loc.replace(/^\?\((.*?)\)$/u, '$1');
this._walk(val, (m) => {
if (this._eval(safeLoc, val[m], m, path, parent, parentPropName)) {
addRet(this._trace(x, val[m], push(path, m), val, m, callback,
true));
}
);
});
} else if (loc[0] === '(') { // [(expr)] (dynamic property/index)
if (this.currPreventEval) {
throw new Error('Eval [(expr)] prevented in JSONPath expression.');
Expand Down Expand Up @@ -543,17 +536,15 @@ JSONPath.prototype._trace = function (
return ret;
};

JSONPath.prototype._walk = function (
loc, expr, val, path, parent, parentPropName, callback, f
) {
JSONPath.prototype._walk = function (val, f) {
if (Array.isArray(val)) {
const n = val.length;
for (let i = 0; i < n; i++) {
f(i, loc, expr, val, path, parent, parentPropName, callback);
f(i);
}
} else if (val && typeof val === 'object') {
Object.keys(val).forEach((m) => {
f(m, loc, expr, val, path, parent, parentPropName, callback);
f(m);
});
}
};
Expand Down