Testing Regex on case vs just switch

Benchmark created on


Description

Tests the difference between:

  • running a regex against each case value and
  • converting a value to lower case before passing it to a switch statement

Setup

const animals = [
  "Lion",
  "Tiger",
  "Elephant",
  "Giraffe",
  "Zebra",
  "Kangaroo",
  "Panda",
  "Koala",
  "Rhinoceros",
  "Hippopotamus",
  "Cheetah",
  "Leopard",
  "Gorilla",
  "Chimpanzee",
  "Orangutan",
  "Wolf",
  "Fox",
  "Bear",
  "Polar Bear",
  "Grizzly Bear",
  "Penguin",
  "Dolphin",
  "Whale",
  "Shark",
  "Octopus",
  "Squid",
  "Seal",
  "Sea Lion",
  "Walrus",
  "Crocodile",
  "Alligator",
  "Tortoise",
  "Turtle",
  "Iguana",
  "Komodo Dragon",
  "Eagle",
  "Falcon",
  "Owl",
  "Parrot",
  "Peacock",
  "Flamingo",
  "Ostrich",
  "Emu",
  "Kangaroo Rat",
  "Meerkat",
  "Lemur",
  "Sloth",
  "Armadillo",
  "Bison",
  "Buffalo"
];

Test runner

Ready to run.

Testing in
TestOps/sec
Run regex.test on each case (match on 50th animal)
const valToMatch = animals[49]

switch (true) {
case /^lion$/i.test(valToMatch): break;
case /^tiger$/i.test(valToMatch): break;
case /^elephant$/i.test(valToMatch): break;
case /^giraffe$/i.test(valToMatch): break;
case /^zebra$/i.test(valToMatch): break;
case /^kangaroo$/i.test(valToMatch): break;
case /^panda$/i.test(valToMatch): break;
case /^koala$/i.test(valToMatch): break;
case /^rhinoceros$/i.test(valToMatch): break;
case /^hippopotamus$/i.test(valToMatch): break;
case /^cheetah$/i.test(valToMatch): break;
case /^leopard$/i.test(valToMatch): break;
case /^gorilla$/i.test(valToMatch): break;
case /^chimpanzee$/i.test(valToMatch): break;
case /^orangutan$/i.test(valToMatch): break;
case /^wolf$/i.test(valToMatch): break;
case /^fox$/i.test(valToMatch): break;
case /^bear$/i.test(valToMatch): break;
case /^polar bear$/i.test(valToMatch): break;
case /^grizzly bear$/i.test(valToMatch): break;
case /^penguin$/i.test(valToMatch): break;
case /^dolphin$/i.test(valToMatch): break;
case /^whale$/i.test(valToMatch): break;
case /^shark$/i.test(valToMatch): break;
case /^octopus$/i.test(valToMatch): break;
case /^squid$/i.test(valToMatch): break;
case /^seal$/i.test(valToMatch): break;
case /^sea lion$/i.test(valToMatch): break;
case /^walrus$/i.test(valToMatch): break;
case /^crocodile$/i.test(valToMatch): break;
case /^alligator$/i.test(valToMatch): break;
case /^tortoise$/i.test(valToMatch): break;
case /^turtle$/i.test(valToMatch): break;
case /^iguana$/i.test(valToMatch): break;
case /^komodo dragon$/i.test(valToMatch): break;
case /^eagle$/i.test(valToMatch): break;
case /^falcon$/i.test(valToMatch): break;
case /^owl$/i.test(valToMatch): break;
case /^parrot$/i.test(valToMatch): break;
case /^peacock$/i.test(valToMatch): break;
case /^flamingo$/i.test(valToMatch): break;
case /^ostrich$/i.test(valToMatch): break;
case /^emu$/i.test(valToMatch): break;
case /^kangaroo rat$/i.test(valToMatch): break;
case /^meerkat$/i.test(valToMatch): break;
case /^lemur$/i.test(valToMatch): break;
case /^sloth$/i.test(valToMatch): break;
case /^armadillo$/i.test(valToMatch): break;
case /^bison$/i.test(valToMatch): break;
case /^buffalo$/i.test(valToMatch): break;
}
ready
Just check value on switch (match on 50th animal)
const valToMatch = animals[49].toLowerCase()

switch (valToMatch) {
case "lion": break;
case "tiger": break;
case "elephant": break;
case "giraffe": break;
case "zebra": break;
case "kangaroo": break;
case "panda": break;
case "koala": break;
case "rhinoceros": break;
case "hippopotamus": break;
case "cheetah": break;
case "leopard": break;
case "gorilla": break;
case "chimpanzee": break;
case "orangutan": break;
case "wolf": break;
case "fox": break;
case "bear": break;
case "polar bear": break;
case "grizzly bear": break;
case "penguin": break;
case "dolphin": break;
case "whale": break;
case "shark": break;
case "octopus": break;
case "squid": break;
case "seal": break;
case "sea lion": break;
case "walrus": break;
case "crocodile": break;
case "alligator": break;
case "tortoise": break;
case "turtle": break;
case "iguana": break;
case "komodo dragon": break;
case "eagle": break;
case "falcon": break;
case "owl": break;
case "parrot": break;
case "peacock": break;
case "flamingo": break;
case "ostrich": break;
case "emu": break;
case "kangaroo rat": break;
case "meerkat": break;
case "lemur": break;
case "sloth": break;
case "armadillo": break;
case "bison": break;
case "buffalo": break;
}
ready
Run regex.test on each case (match on first animal)
const valToMatch = animals[0]

switch (true) {
case /^lion$/i.test(valToMatch): break;
case /^tiger$/i.test(valToMatch): break;
case /^elephant$/i.test(valToMatch): break;
case /^giraffe$/i.test(valToMatch): break;
case /^zebra$/i.test(valToMatch): break;
case /^kangaroo$/i.test(valToMatch): break;
case /^panda$/i.test(valToMatch): break;
case /^koala$/i.test(valToMatch): break;
case /^rhinoceros$/i.test(valToMatch): break;
case /^hippopotamus$/i.test(valToMatch): break;
case /^cheetah$/i.test(valToMatch): break;
case /^leopard$/i.test(valToMatch): break;
case /^gorilla$/i.test(valToMatch): break;
case /^chimpanzee$/i.test(valToMatch): break;
case /^orangutan$/i.test(valToMatch): break;
case /^wolf$/i.test(valToMatch): break;
case /^fox$/i.test(valToMatch): break;
case /^bear$/i.test(valToMatch): break;
case /^polar bear$/i.test(valToMatch): break;
case /^grizzly bear$/i.test(valToMatch): break;
case /^penguin$/i.test(valToMatch): break;
case /^dolphin$/i.test(valToMatch): break;
case /^whale$/i.test(valToMatch): break;
case /^shark$/i.test(valToMatch): break;
case /^octopus$/i.test(valToMatch): break;
case /^squid$/i.test(valToMatch): break;
case /^seal$/i.test(valToMatch): break;
case /^sea lion$/i.test(valToMatch): break;
case /^walrus$/i.test(valToMatch): break;
case /^crocodile$/i.test(valToMatch): break;
case /^alligator$/i.test(valToMatch): break;
case /^tortoise$/i.test(valToMatch): break;
case /^turtle$/i.test(valToMatch): break;
case /^iguana$/i.test(valToMatch): break;
case /^komodo dragon$/i.test(valToMatch): break;
case /^eagle$/i.test(valToMatch): break;
case /^falcon$/i.test(valToMatch): break;
case /^owl$/i.test(valToMatch): break;
case /^parrot$/i.test(valToMatch): break;
case /^peacock$/i.test(valToMatch): break;
case /^flamingo$/i.test(valToMatch): break;
case /^ostrich$/i.test(valToMatch): break;
case /^emu$/i.test(valToMatch): break;
case /^kangaroo rat$/i.test(valToMatch): break;
case /^meerkat$/i.test(valToMatch): break;
case /^lemur$/i.test(valToMatch): break;
case /^sloth$/i.test(valToMatch): break;
case /^armadillo$/i.test(valToMatch): break;
case /^bison$/i.test(valToMatch): break;
case /^buffalo$/i.test(valToMatch): break;
}
ready
Just check value on switch (match on first animal)
const valToMatch = animals[0].toLowerCase()

switch (valToMatch) {
case "lion": break;
case "tiger": break;
case "elephant": break;
case "giraffe": break;
case "zebra": break;
case "kangaroo": break;
case "panda": break;
case "koala": break;
case "rhinoceros": break;
case "hippopotamus": break;
case "cheetah": break;
case "leopard": break;
case "gorilla": break;
case "chimpanzee": break;
case "orangutan": break;
case "wolf": break;
case "fox": break;
case "bear": break;
case "polar bear": break;
case "grizzly bear": break;
case "penguin": break;
case "dolphin": break;
case "whale": break;
case "shark": break;
case "octopus": break;
case "squid": break;
case "seal": break;
case "sea lion": break;
case "walrus": break;
case "crocodile": break;
case "alligator": break;
case "tortoise": break;
case "turtle": break;
case "iguana": break;
case "komodo dragon": break;
case "eagle": break;
case "falcon": break;
case "owl": break;
case "parrot": break;
case "peacock": break;
case "flamingo": break;
case "ostrich": break;
case "emu": break;
case "kangaroo rat": break;
case "meerkat": break;
case "lemur": break;
case "sloth": break;
case "armadillo": break;
case "bison": break;
case "buffalo": break;
}
ready

Revisions

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