Skip to content
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

Performance improvements #220

Open
romulof opened this issue Oct 2, 2024 · 0 comments
Open

Performance improvements #220

romulof opened this issue Oct 2, 2024 · 0 comments
Labels

Comments

@romulof
Copy link

romulof commented Oct 2, 2024

Motivation

Performance

Current behavior

Each time I pass a path it needs to be parsed and the queried for all possible matching items

Desired behavior

Be able to create and object for a path that stores the parsed query and allows me to execute it many times, skipping the overhead of path parsing. Additionally it would be good to specify the search limit.

Example:

const bookTitlePath = new JSONPath<string>('$..book[*].title');
const bookPricePath = new JSONPath<number>('$..book[*].price');

function getFirstBook(storeData: object): string {
  return bookTitlePath.value(storeData); // Returns first match
}

// Consider books are in desc order of sales
function getAveragePriceOfBestSellingBooks(storeData: object): number {
  const prices: number[] = bookPricePath.query(storeData, 10); // Get up to 10 elements

  // The only viable usage of reduce 😬 
  return prices.reduce((total, price) => total + price, 0) / prices.length;
}

Alternatives considered

Could not find any.

@romulof romulof added the Feature label Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant