arr find vs keyBy + map

Benchmark created on


Preparation HTML

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

Setup

const len = 4000;

const dt = new Date().toISOString();

function getItem(i) {
	return {
 		promotionId: i,
 		itemId: 10_000 + i,
		attributes: {
    		foo	: 'bar',
		    bar: 'baz'
	  	},
	  	updatedDate: dt,
  		itemGroupId: '1234789',
  		isActiveInGroup: true,
  		totalCount: 1000,
	};				
}

const arr = new Array(len);
for (let i = 0; i < len; ++i) {
	arr[i] = getItem(i);
}


const updates = new Array(100).fill(0).map((item, index) => ({ itemId: 10_000 + Math.floor(Math.random() * len), update: 'somedata' }));

Test runner

Ready to run.

Testing in
TestOps/sec
keyBy
const lookup = _.keyBy(arr, 'itemId');
​
const values = updates.map(({ itemId }) => lookup[itemId]);


ready
arr find
const values = updates.map(({ itemId }) => arr.find(val => val.itemId === itemId));


ready

Revisions

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