reduce vs foreach (v2)

Revision 2 of this benchmark created on


Description

runs some tests against building a record with imperative foreach vs functional reduce

Setup

const dbcols = [
	{schema: "public", table: "users"},
	{schema: "public", table: "accounts"},
	{schema: "public", table: "foo"},
  	{schema: "public", table: "users"},
	{schema: "public", table: "accounts"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts"},
	{schema: "public", table: "foo"},
]
const subsets = [
	{schema: "public", table: "users"},
	{schema: "public", table: "accounts", whereClause: "foo"},
	{schema: "public", table: "foo"},
  	{schema: "public", table: "users"},
	{schema: "public", table: "accounts", whereClause: "foo"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts", whereClause: "foo"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts", whereClause: "foo"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts", whereClause: "foo"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts", whereClause: "foo"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts", whereClause: "foo"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts", whereClause: "foo"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts", whereClause: "foo"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts", whereClause: "foo"},
	{schema: "public", table: "foo"},	{schema: "public", table: "users"},
	{schema: "public", table: "accounts", whereClause: "foo"},
	{schema: "public", table: "foo"},
]
function buildRowKey(schema, table) {
  return `${schema}.${table}`;
}

Test runner

Ready to run.

Testing in
TestOps/sec
foreach


function buildTableRowData1(
  dbCols,
  existingSubsets
) {
  const tableMap = {};

  dbCols.forEach((mapping) => {
    const key = buildRowKey(mapping.schema, mapping.table);
     tableMap[key] = { schema: mapping.schema, table: mapping.table };
   });
   existingSubsets.forEach((subset) => {
     const key = buildRowKey(subset.schema, subset.table);
     if (tableMap[key]) {
       tableMap[key].where = subset.whereClause;
     }
   });

   return tableMap;
}
buildTableRowData1(dbcols, subsets);
ready
reduce
function buildTableRowData2(
  dbCols,
  existingSubsets
) {
  const tableMap = dbCols.reduce(
    (map, mapping) => {
      const key = buildRowKey(mapping.schema, mapping.table);
      return {
        ...map,
        [key]: { schema: mapping.schema, table: mapping.table },
      };
    },
    {}
  );

  return existingSubsets.reduce((map, subset) => {
    const key = buildRowKey(subset.schema, subset.table);
    if (map[key]) {
      return {
        ...map,
        [key]: {
          ...map[key],
          where: subset.whereClause,
        },
      };
    }
    return map;
  }, tableMap);
}
buildTableRowData2(dbcols, subsets);

ready

Revisions

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