Q:

Performant way to compare two array of objects with v8 & filter/find

I have 2(well, > 2) arrays of objects with the structure like this:
const mainArr = [
  { article: "somename-1", price: 10, found: [] },
  { article: "somename-2", price: 20, found: [] },...
];
const toCompareArr = [
  { article: "somename-1", articleFormatted: "0somename-1", price: 20 },
  { article: "somename-3", articleFormatted: "0somename-3", price: 20 },...
];

And for mainArr i try to find all occurencies in 2nd toCompareArr array (basically - I need to create price comparator for different suppliers), like this:
let i = 0;
let j = 0;
const iMax = mainArr.length;
const jMax = toCompareArr.length;
for (; i < iMax; i++) {
  for (; j < jMax; j++) {
    if (
      mainArr[i].article === toCompareArr[j].article ||
      mainArr[i].article === `00-${toCompareArr[j].articleFormatted}`
    ) {
      mainArr[i].found.push(toCompareArr[j]);
    }
  }

  j = 0;
}

// also tried like this (for + for seems to be more speedy):
/*
let found;
for (; i < iMax; i++) {
  found = toCompareArr.filter(
    item =>
      item.article === mainArr[i].article ||
      item.articleFormatted === mainArr[i].article
  );

  if (found) {
    mainArr[i].found.push(found);
  }
}
*/

as result I want to get:
[
  {
    article: "somename-1",
    price: 10,
    found: [
      { article: "somename-1", articleFormatted: "0somename-1", price: 20 }
    ]
  },
  { article: "somename-2", price: 20, found: [] }
];

but it's extremely slow when I have a lot of data (normally there would be around 80k-100k array items in each)
And I stuck... Maybe there is a way to improve performance of filter & find here? I'm using latest Node & V8 features. Now it took around 5min to compare, if it would be 50% (let's say 2min - it's way better)
maybe somehow I should totally reinvent my logic? use some algorithms (would like to know - which one) 

A:

It's extremely slow when I have a lot of data - normally there would be around 80k-100k array items in each

Use a database for this task. A good DBMS should be able to do this comparsion is less than a second, using a hash join or similar technique. Of course you can re-implement that in node.js if you insist - use the article identifier as the key in a Map.

A:

You could take a Map.

const
    mainArr = [{ article: "somename-1", price: 10, found: [] }, { article: "somename-2", price: 20, found: [] }],
    toCompareArr = [{ article: "somename-1", articleFormatted: "0somename-1", price: 20 }, { article: "somename-3", articleFormatted: "0somename-3", price: 20 }],
    map = mainArr.reduce((m, o) => m.set(o.article, o), new Map);

for (let item of toCompareArr) {
    let object = map.get(item.article) || map.get('00-' + item.articleFormatted);
    if (!object) continue;
    object.found.push(item);
}

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

