Reduce vs For

Benchmark created on


Setup

const FIELDS = [];

for (let i = 0; i < 1000; i++) {
	FIELDS.push({ id: i, value: i})
}

Test runner

Ready to run.

Testing in
TestOps/sec
Using for loop
  const customFieldObject = [];
  let i = 0;

  for (const key in FIELDS) {
    if (FIELDS[key]?.value % 2) {
      const newObj = {
        id: Number(key),
        value: FIELDS[key]?.value || [],
      };
      customFieldObject[i++] = newObj;
    }
  }
ready
Using Reduce
  const customFieldObject = FIELDS.reduce((acc, field, index) => {
  	    if (field.value % 2) {
      const newObj = {
        id: Number(index),
        value: field.value || [],
      };
      acc.push(newObj);
        }
      return acc;
  }, []);
ready

Revisions

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