Álvaro
Álvaro

Reputation: 2598

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; }

Upvotes: 0

Views: 166

Answers (1)

Barmar
Barmar

Reputation: 781706

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;
}

Upvotes: 1

Related Questions