Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Intersection of 2 arrays containing objects

I’m using the below code to intersect 2 very large arrays. Is it possible to improve the performance of this as it takes such a long time as it currently stands.

const arr1 = [  
    {  
       "number":"123",
       "firstName":"John",
       "lastName":"Smith",
       "email":"test1@test.com",
    },
    {  
       "number":"1234",
       "firstName":"Chad",
       "lastName":"Baker",
       "email":"test2@test.com",
    }
];
 
const arr2 = [  
    {  
        "number":"12345",
        "firstName":"Chad",
        "lastName":"Baker",
        "email":"test2@test.com",
    },
    {  
        "number":"123456",
        "firstName":"John",
        "lastName":"Smith",
        "email":"test1@test.com",
    }
]

 let arr3 = arr1.filter(a => arr2.some(b => { return a.firstName == b.firstName && a.lastName == b.lastName}));  
 
 console.log(arr3);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Create an id based on your matching criterion and add it to a Set for one of your arrays. Then you have a constant lookup time

let createId = (x) => `F:${x.firstName};L:${x.lastName}`;

//ids is a Set<string> which will only contain values created by the createId function
let ids = new Set(arr2.map(x => createId(x)));

//now in the filter, you have a set-lookup which is O(1) instead of 
//a linear search which is O(n)
let intersection = arr1.filter(x => ids.has(createId(x)));

Of course, you can modify the createId however you want. By calling it also in the callback of filter, you make sure, all objects are treated the same.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading