Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
wait while loop is over

I have an arrow function which get two arrays as parameters then on certain condition i push items from second array to first array

The problem is that i have big amount of data that i should to push and res.send cant wait while loop is over and send me error about memory JavaScript heap out of memory

How i can wait while my loop is over and make loop to work faster? I just try with async/await, promiseAll, for await

let mergeById = async(arr1, arr2) => {
  arr1.map(async(item) => {
    item.properties = [];
    await arr2.map(async(prop) => {
      if (item.item_id == prop.item_id) {
        await item.properties.push(prop);
      }
    })
  })
  return arr1
}

async someFunction(req, res) {

  arr1 = dataItems(); //6000 items
  arr2 = dataProps(); //60000 items

  let result = await mergeById(arr1, arr2);

  res.json({
    data: result
  })

}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have map inside map, therefore for each item in array1 it will iterate over whole array 2. You have encountered O(n^2) complexity and as you can see - even with 66 000 items (which is not that much for computer on its own), you are encountering huge impact. Because you have to do 6 000 * 60 000 = 360,000,000 iterations

There is a solution. You have to do some preprocessing - create new Map() for array2, iterate over all items and save item_id as a key with value true. Something like array2Map.set(prop.item_id, prop)

Then change your cycle to check your map

arr1.map(async(item) => {
    item.properties = [];
    const prop = array2Map.get(item.item_id);
    if (prop) {
        item.properties.push(prop)
    }

Now you have O(n * log n) complexity (well its almost O(n) because of pseudo-linear complexity of hashmaps, but thats different story, not that important now). And thats good enough for your use-case.


Also there is another problem with usage of the await and async. Just remove all this usage from all the code you have presented. It does not do anything useful, but actually breaks few things.

.map function runs synchronously no matter if you put async function as a parameter. It will just create an array of promises. Unless you know exactly what you are doing, my tip is that for array functions (map, foreEach, reduce etc.) never ever use async function as parameter - as it will mostly not work as you expect.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda