`find` in loop vs `indexby` in advance

Benchmark created on


Preparation HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.30.0/ramda.min.js"></script>

Setup

const generateObj = (id) => ({ id });

const nestedArr10 = new Array(10);
const testArr10 = new Array(10);
let i = 1;
while (i < 10) {
  const obj = generateObj(i++);
  nestedArr10[i - 1] = { obj };
  testArr10[10 - i] = { ...obj };
}

const nestedArr100 = new Array(100);
const testArr100 = new Array(100);
i = 1;
while (i < 100) {
  const obj = generateObj(i++);
  nestedArr100[i - 1] = { obj };
  testArr100[100 - i] = { ...obj };
}

const nestedArr1000 = new Array(1000);
const testArr1000 = new Array(1000);
i = 1;
while (i < 1000) {
  const obj = generateObj(i++);
  nestedArr1000[i - 1] = { obj };
  testArr1000[1000 - i] = { ...obj };
}

Test runner

Ready to run.

Testing in
TestOps/sec
".find" 10
const res = nestedArr10.map((testObj) => {
      const x = testArr10.find((y) => y.id === testObj.obj.id);
      return x;
    });
ready
"indexBy" 10
const testMap = R.indexBy(R.prop('id'), testArr10);
    const res = nestedArr10.map((testObj) => {
      const x = testMap[testObj.obj.id];
      return x;
    });
ready
".find" 100
const res = nestedArr100.map((testObj) => {
      const x = testArr100.find((y) => y.id === testObj.obj.id);
      return x;
    });
ready
"indexBy" 100
const testMap = R.indexBy(R.prop('id'), testArr100);
    const res = nestedArr100.map((testObj) => {
      const x = testMap[testObj.obj.id];
      return x;
    });
ready
".find" 1000
const res = nestedArr1000.map((testObj) => {
      const x = testArr1000.find((y) => y.id === testObj.obj.id);
      return x;
    });
ready
"indexBy" 1000
const testMap = R.indexBy(R.prop('id'), testArr1000);
    const res = nestedArr1000.map((testObj) => {
      const x = testMap[testObj.obj.id];
      return x;
    });
ready

Revisions

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