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

163
Visualizações
Reduce inside map inside map not efficient

I am trying to run a series of keywords against a series of categories and then within those categories there are some options. So I have ended up doing a map over map over a reduce and when dealing with a lot of entries node consumes way too much memory.

I got this, what it does is not really the problem, but how to make it not crave for such amounts of memory?

const keywords = [
  {
    Keyword: 'foo',
    URL: 'https://www.facebook.co.uk'
  },
  {
    Keyword: 'foo',
    URL: 'https://www.twitter.co.uk/blue'
  },
  {
    Keyword: 'faa',
    URL: 'https://www.facebook.co.uk/twitter'
  },
  {
    Keyword: 'faa',
    URL: 'https://www.apple.co.uk/green'
  }
]

const categories = [
  {
    name: 'Tech',
    options: [
      {
        method: 'include',
        regex: 'facebook'
      },
      {
        method: 'exclude',
        regex: 'twitter'
      }
    ]
  },
  {
    name: 'Green',
    options: [
      {
        method: 'include',
        regex: 'green'
      }
    ]
  }
]

const result = keywords.map((obj) => {
  categories.map(({ name, options }) => {
    const option = options.reduce((acc, { method, regex }) => {
      acc.push(method === 'include' ? obj.URL.includes(regex) : !obj.URL.includes(regex))
      return acc
    }, [])

    obj[name] = !option.includes(false)
  })

  return obj
})

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

0

You're creating lots of arrays that aren't needed.

If you only care if option includes false(), you can replace the reduce() call with every().

categories.map() should be categories.forEach(), since you don't use the resulting array.

Since you're modifying obj in place and returning this, the objects in the result array will be the same as in the keywords array, so there's no need to create a new array there, either. So you could use forEach() as well.

const keywords = [{
    Keyword: 'foo',
    URL: 'https://www.facebook.co.uk'
  },
  {
    Keyword: 'foo',
    URL: 'https://www.twitter.co.uk/blue'
  },
  {
    Keyword: 'faa',
    URL: 'https://www.facebook.co.uk/twitter'
  },
  {
    Keyword: 'faa',
    URL: 'https://www.apple.co.uk/green'
  }
]

const categories = [{
    name: 'Tech',
    options: [{
        method: 'include',
        regex: 'facebook'
      },
      {
        method: 'exclude',
        regex: 'twitter'
      }
    ]
  },
  {
    name: 'Green',
    options: [{
      method: 'include',
      regex: 'green'
    }]
  }
]

keywords.forEach((obj) =>
  categories.forEach(({name, options}) => 
    obj[name] = options.every(({method, regex}) => 
      method === 'include' ? obj.URL.includes(regex) : !obj.URL.includes(regex))))

console.log(keywords)
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

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