Grouping - object vs map

Benchmark created on


Setup

function basename(path) {
   return path.substring(path.lastIndexOf("/") + 1)
}

function groupFilesMap(filePaths) {
  const groups = new Map();

  for (const filePath of filePaths) {
    const filename = basename(filePath);
    const [prefix, ...suffixes] = filename.split('.');
    let group = groups.get(prefix);

    if (!group) {
      group = {path: filePath, children: []};
      groups.set(prefix, group);
    }

    if (suffixes[0] == 'razor' || suffixes[0] == 'cshtml') {
      if (suffixes.length == 1) {
        group.path = filePath;
      } else {
        group.children.push(filePath);
      }
    }
  }

  return groups.values();
}

function groupFilesObject(filePaths) {
  const groups = {};

  for (const filePath of filePaths) {
    const filename = basename(filePath);
    const [prefix, ...suffixes] = filename.split('.');
    let group = groups[prefix];

    if (!group) {
      group = groups[prefix] = {path: filePath, children: []};
    }

    if (suffixes[0] == 'razor' || suffixes[0] == 'cshtml') {
      if (suffixes.length == 1) {
        group.path = filePath;
      } else {
        group.children.push(filePath);
      }
    }
  }

  return Object.values(groups);
}

const data = ["/Index.razor", "/Index.razor.js", "/Index.razor.css", "/foo", "/bar", "/baz"];

Test runner

Ready to run.

Testing in
TestOps/sec
Map
groupFilesMap(data)
ready
Object
groupFilesObject(data)
ready

Revisions

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