K
K
Kizzeon2020-03-05 02:15:45
JavaScript
Kizzeon, 2020-03-05 02:15:45

What is the fastest way to search for data in a JSON file?

Now I'm doing a database search of the form:

var matches = array.filter(array=> {
    const regex = new RegExp(`${searchText}`, 'gi'),
          stateName = state.name,
          stateData = String(state.data);
    return stateName.match(regex) || stateData.match(regex); // здесь дольше всего
  });


But it takes terribly long to find the data

Please advise how to optimize this code

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Cheremkhin, 2020-03-05
@Kizzeon

const searchText = ‘....’;
const regex = new RegExp(searchText, 'gi'); // вынести за цикл filter

var matches = array.filter(state=> {
  const name = state.name, 
         data =  state.data.toString();
    return regex.test(name) || regex.test(data);
});

I
Interface, 2020-03-05
@Interface

If your "json" is a field coming from postgress's jsonb field, then postgress can make requests inside jsonb: https://hackernoon.com/how-to-query-jsonb-beginner... (first link in google).
If this option does not fit, the obvious optimization is to stick the creation of the regexp out of the loop. Though I'm not sure which v8 thread has unoptimized this on its own. (on my machine, this cuts the time by about a third)
How much data do you have? Searching across multiple strings shouldn't take long. And as long as you don't have millions of these lines, everything should be fast. The equivalent code on my machine does a regexp pass through an array of a million strings in less than 100ms. If you have larger volumes or at least comparable ones, you are doing something wrong :) Well, or if you expect such a volume of data to be processed instantly, then you should adjust your expectations.
Ps is it node or browser? If it’s a node, then dancing on the js side doesn’t make sense anyway, since it won’t be possible to speed up radically, but 2-3 users looking for something will give you everything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question