loop regex

Benchmark created on


Setup

const APPROVED_FUNCTIONAL_WILDCARD_KEYS = [
  { pattern: /^dashboard_impressions-/, key: "dashboard_impressions-*" },
  {
    pattern: /^grafana\.dashboard\.editor\.ui\.optionGroup/,
    key: "grafana.dashboard.editor.ui.optionGroup[*]",
  },
  {
    pattern: /^grafana\.explore\.datasource/,
    key: "grafana.explore.datasource.*",
  },
  { pattern: /^grafana\.explore\.history/, key: "grafana.explore.history.*" },
  {
    pattern: /^grafana\.navigation\.expanded/,
    key: "grafana.navigation.expanded[*]",
  },
  { pattern: /^search\.sections/, key: "search.sections.*" },
];

const lookup = {};
APPROVED_FUNCTIONAL_WILDCARD_KEYS.forEach((wildcard) => {
  lookup[wildcard.key] = { category: "functional" };
});

function isApprovedKey(key) {
  for (const wildcard of APPROVED_FUNCTIONAL_WILDCARD_KEYS) {
    // test the `key` argument against the pattern
    if (wildcard.pattern.test(key)) {
      // check for consent using the registered/approved wildcard key
      return !!lookup[wildcard.key];
    }
  }
  return false;
}

const lookup2 = new Set(APPROVED_FUNCTIONAL_WILDCARD_KEYS.map((wc) => wc.key));
function isApprovedKey2(key) {
  return lookup2.has(key);
}

Test runner

Ready to run.

Testing in
TestOps/sec
how long does it take to a pattern match an unknown key in a loop
isApprovedKey('unknown');
ready
how long does it take to a pattern match a known key in a loop
isApprovedKey('search.sections');
ready
how long does it take to a pattern match an unknown key in a set
isApprovedKey2('unknown');
ready
how long does it take to a pattern match a known key in a set
isApprovedKey2('search.sections');
ready

Revisions

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