Lodash intersection vs nested finds

Benchmark created on


Description

This benchmark compares some algorithms to check for the presence of the same elements in two different lists.

Preparation HTML

<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>

Setup

const makeid = (len = 5) => {
  let text = "";
  let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (let i = 0; i < len; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}

let arr1 = []
let arr2 = []

for (let i = 0; i < 1000; i++) {
	arr1.push(makeid())
  	arr2.push(makeid())
}

// To ensure that both arrays has at least 1 element in common
arr1[arr1.length - 1] = arr2[0]

Test runner

Ready to run.

Testing in
TestOps/sec
1) Lodash isEmpty and intersection
return !_.isEmpty(_.intersection(arr1, arr2))
ready
2) Nested finds
return Boolean(
    arr1.find((el1) =>
      arr2.find((el2) => {
        return (
          el1 === el2
        );
      })
    )
  );
ready

Revisions

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