Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!