Check an array against another with performance

I got an initial array, I am checking against another array to find how many objects have at least one instance of the Domain in data.

This works but it performs very poorly when there is a lot of data.

const data = [
  {
    Domain: 'google.com',
    '# Reocurring Domains': 0
  },
  {
    Domain: 'apple.com',
    '# Reocurring Domains': 0
  },
  {
    Domain: 'facebook.com',
    '# Reocurring Domains': 0
  }
]

const domains = [
  {
    'google.com': true,
    'microsoft.com': true,
    'google.com': true
  },
  {
    'apple.com': true,
    'microsoft.com': true,
    'twitter.com': true
  },
  {
    'facebook.com': true,
    'apple.com': true,
    'facebook.com': true
  }
]

for (const obj of data) {
  let count = 1
  for (const entry of domains) {
    if (entry[obj.Domain]) {
      obj['# Reocurring Domains'] = count++
    }
  }
}

console.log(data)

In there any way to this with a more performant approach?

Thanks.

23 thoughts on “Check an array against another with performance”

  1. Go through first and index the domains

    const domains = [
      {
        'google.com': true,
        'microsoft.com': true,
        'google.com': true
      },
      {
        'apple.com': true,
        'microsoft.com': true,
        'twitter.com': true
      },
      {
        'facebook.com': true,
        'apple.com': true,
        'facebook.com': true
      }
    ]
    
    const domainIndex = {};
    for (const entry of domains) {
        for(const domain of Object.keys(entry))
            domainIndex[domain] = (domainIndex[domain] || 0) +1 
    }
    console.log(domainIndex);

    This will make it much quicker when trying to look up how many domains for each element in your data array as there is no longer a need for a nested loop.

    const data = [
      {
        Domain: 'google.com',
        '# Reocurring Domains': 0
      },
      {
        Domain: 'apple.com',
        '# Reocurring Domains': 0
      },
      {
        Domain: 'facebook.com',
        '# Reocurring Domains': 0
      }
    ]
    
    const domains = [
      {
        'google.com': true,
        'microsoft.com': true,
        'google.com': true
      },
      {
        'apple.com': true,
        'microsoft.com': true,
        'twitter.com': true
      },
      {
        'facebook.com': true,
        'apple.com': true,
        'facebook.com': true
      }
    ]
    const domainIndex = {};
    for (const entry of domains) {
        for(const domain of Object.keys(entry))
            domainIndex[domain] = (domainIndex[domain] || 0) +1 
    }
    
    for (const obj of data) {
        obj['# Reocurring Domains'] = domainIndex[obj.Domain] 
    }
     console.log(data);

    It is important to note that with this solution that it separates indexing the data from looking up the data. When comparing to your original you shouldn’t include the time taken to index (which only needs to be done once when you get the domain data).

    Performance comparison: https://jsbench.me/49kl892vlf/1

    Reply
  2. Maybe you can replace the inner loop using a filter function

    for (const obj of data) {
      
      const filteredDomains = domains.filter((entry)=>{
          // code for filtering
         })
     
      obj['# Reocurring Domains'] = filteredDomains.length
    
    
    }
    
    Reply

Leave a Comment