contains-common-item

Benchmark created on


Setup

const array1 = ['a', 'b', 'c', 'x'];
const array2 = ['z', 'y', 'a'];

Test runner

Ready to run.

Testing in
TestOps/sec
containsCommonItem
function containsCommonItem(arr1, arr2) {
  for (let i=0; i < arr1.length; i++) {
    for ( let j=0; j < arr2.length; j++) {
      if(arr1[i] === arr2[j]) {
        return true;
      }
    }
  }
  return false
}

containsCommonItem(array1, array2)
ready
containsCommonItem2
function containsCommonItem2(arr1, arr2) {
  // loop through first array and create object where properties === items in the array
  // can we assume always 2 params?

  let map = {};
  for (let i=0; i < arr1.length; i++) {
    if(!map[arr1[i]]) {
      const item = arr1[i];
      map[item] = true;
    }
  }
  // loop through second array and check if item in second array exists on created object. 
  for (let j=0; j < arr2.length; j++) {
    if (map[arr2[j]]) {
      return true;
    }
  }
  return false
}

containsCommonItem2(array1, array2)
ready
containsCommonItem3
function containsCommonItem3(arr1, arr2) {
  return arr1.some(item => arr2.includes(item))
}

containsCommonItem3(array1, array2)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.