Lodash diff vs native diff (v4)

Revision 4 of this benchmark created on


Preparation HTML

<script src="
https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js
"></script>

Setup

function getRandomInt(min, max) {
  const minCeiled = Math.ceil(min);
  const maxFloored = Math.floor(max);
  return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); 
}

const AMOUNT = 1_000_000;
const arr1 = Array(AMOUNT).fill(getRandomInt(100000 - 999999));

const arr2 = Array(AMOUNT).fill(getRandomInt(100000 - 999999));

Test runner

Ready to run.

Testing in
TestOps/sec
Lodash diff
const diff = _.difference(arr1, arr2);
ready
Native diff
const arr2Set = new Set(arr2);
const arr1Len = arr1.length;
const diff = [];
for (let i=0; i<arr1Len; i++) {
	if (!arr2Set.has(arr1[i])) {
		diff.push(arr2[i]);
	}
}
ready

Revisions

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