Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
js search in array with 50k result is so slow

i have web app pos the array filter search in client side in browser not in the server first i download all products as array and work on it and its working so good with number of products under 20k since a while the array of the products get the number of 50k

and my results array like this ..

   [
      {
        "id": 1,
        "name": "Product 1",
        "number": "1",
        "available_on_pos": 1,
        "pos_quick_lunch": 1,
        "product_category_id": 1,
        "discount_value": 10,
        "smallest_unit_id": 1,
        "smallest_unit_barcode": null,
        "smallest_unit_selling_price": 10,
        "smallest_unit_selling_price_above": null,
        "smallest_unit_selling_price_above_price": null,
        "tax_value": 16,
        "get_smallest_unit_id": {
          "id": 1,
          "name": "Unit"
        },
        "get_price_list_lines": []
      },
      {
        "id": 13,
        "name": "Product 2",
        "number": "13",
        "available_on_pos": 1,
        "pos_quick_lunch": null,
        "product_category_id": 1,
        "discount_value": null,
        "smallest_unit_id": 1,
        "smallest_unit_barcode": "9501025172815",
        "smallest_unit_selling_price": 0.509,
        "smallest_unit_selling_price_above": null,
        "smallest_unit_selling_price_above_price": null,
        "tax_value": 16,
        "get_smallest_unit_id": {
          "id": 1,
          "name": "Unit"
        },
        "get_price_list_lines": []
      }
    ]

and this is the search method

var results = this.products.filter(obj => obj.smallest_unit_barcode != null && obj.smallest_unit_barcode.includes(value));
this.pos_quick_lunch = results;

as i say the the products array is 50k the search like have delay when i press barcode gun multiable time at same product search and this is long time in pos to wait .. is there any way thats i can make the search much easer and faster

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

is there any way thats i can make the search much easer and faster

var results = this.products.filter(obj => obj.smallest_unit_barcode != null && obj.smallest_unit_barcode.includes(value));

As long as we keep the focus on this part of code and don't start discussions about general design ideas, I see a these options...

Cache the undefined case

Your could create a list of products with smalles_unit_barcode != null or undefined and keep it. This should give you a small improvement.

// do this only when products collection changes
const this.cachedProductsWithBarcode = this.products.filter(p => !!p.smallest_unit_barcode);

// do this on every request / input
const results = this.cachedProductsWithBarcode(p => p.smallest_unit_barcode.includes(value));

Do you need partial matches?

If you get the full article number / gtin, there's no need to do a partial search and you could use a dictionary lookup instead.

// only when products collection changes
const dict = this.products.reduce((prev, cur) => (prev[cur.smalles_unit_barcode] = cur, prev), {});

// for each request
return dict[value];

(If you get the full barcode most of the time but some times just a part of it, you could think about doing a lookup first and continue with a search if you have no match.)

Delay user input / Cache

If you search needle comes from a user input, where users can type the barcode and you do this search on every keystroke this search will block the UI making the app unusable. Delaying the user input (= do the search only if we get no more key strokes for e.g. 500ms) could improve the user experience.

In addition you could cache results

// User input "123"
// const firstResult = products.find(...)

// User input "1234"
// ... must be in firstResult, so let' search there
return firstResult.find(...)

(This makes only sense if it's 'search while typing' and you need the invalidate the cache if needle allows more results than cached results list.)

Misc...

  • Use startsWith() instead of includes() if possible (expect about 10% improvement) - if you can't use ==
  • Limit the number of results. In many cases you need only ONE result, so use find() instead. You should never need more than ~10 results, so stop filtering when this is reached.
  • Separate search and lookup in your UI. From my experience some people are really fast typing article codes + ENTER - (= full code = dict lookup = fast) but sometimes they don't know the full code and want a to search (= partial code = slow). Separating these two use cases can be as simple as using an input field with two buttons ("ok", "search" and ENTER = "ok"). Or you use a flow like type in input => ENTER => lookup full code in dict => not found? => show "not found, here are some results..."
about 4 years ago · Juan Pablo Isaza Relatório

0

It doest clear if you are talking about client js or a node.js server.

if you are talking about js run on your browser I think a much better approach is to make the back end do the filtering. I do not think a client should gets such a mount of data.

if you dealing with a node.js server you should consider improve your database query and let it do the filtering and not query the whole data.

about 4 years ago · Juan Pablo Isaza Relatório

0

Product catalog search is generally done from the backend server side (especially since your dataset of products is so large and not just a few). You would need to implement a search field and button that calls an API. Also the results should be paginated, meaning you do not want to load all 50k products into the browser's memory or you will easily run into issues (especially in mobile devices) with app cache.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda