Lodash diff vs native diff

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 arr1Set = new Set(arr1);
const arr2Len = arr2.length;
const diff = [];
for (let i=0; i<arr2Len; i++) {
	if (!arr1Set.has(arr2[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.