A

Benchmark created on


Setup

const params = {
  deductionTypes: ['type1', 'type2'],
  reasons: ['reason1', 'reason2'],
};

const createMockData = (numRows = 1000, warningsPerRow = 5) => {
  return Array.from({ length: numRows }, (_, i) => ({
    id: i,
    otherData: `row${i}`,
    warnings: Array.from({ length: warningsPerRow }, (_, j) => ({
      deduction_type: j % 2 === 0 ? 'type1' : 'typeX',
      reason: j % 3 === 0 ? 'reason1' : 'reasonX',
      warningDetail: `warn${j}`,
    })),
  }));
};

const data = createMockData();

Test runner

Ready to run.

Testing in
TestOps/sec
AA
function originalVersion(result) {
  return result.flatMap((row) =>
    row.warnings
      .filter((warning) => {
        if (
          params.deductionTypes &&
          params.deductionTypes.length > 0 &&
          !params.deductionTypes.includes(warning.deduction_type)
        ) {
          return false;
        }
        if (params.reasons && params.reasons.length > 0 && !params.reasons.includes(warning.reason)) {
          return false;
        }
        return true;
      })
      .map((warning) => ({
        ...row,
        ...warning,
      }))
  );
}


  originalVersion(data);
ready
BB
function optimizedVersion(result) {
  const output = [];

  for (const row of result) {
    for (const warning of row.warnings) {
      const includeDeductionType =
        !params.deductionTypes?.length || params.deductionTypes.includes(warning.deduction_type);
      const includeReason =
        !params.reasons?.length || params.reasons.includes(warning.reason);

      if (includeDeductionType && includeReason) {
        output.push({ ...row, ...warning });
      }
    }
  }

  return output;
}

  optimizedVersion(data);
ready

Revisions

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