regex vs default

JavaScript performance comparison

Test case created by HKGx

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const usersObj = {"525752440230772767": {"username": "Gabrielle", "id": 525752440230772767}, "327885457150967809": {"username": "Hexu", "id": 327885457150967809}, "265807190936387584": {"username": "MinimumHaker", "id": 265807190936387584}, "300171496897118221": {"username": "JakisTamKuba", "id": 300171496897118221}, "195935967440404480": {"username": "Predator", "id": 195935967440404480}, "242730576195354624": {"username": "Auttaja", "id": 242730576195354624}, "219926422297247754": {"username": "Kebappa", "id": 219926422297247754}, "358254847872598027": {"username": "Kostak_", "id": 358254847872598027}, "329694416472375298": {"username": "Creeperowy Joneg", "id": 329694416472375298}, "356867274248028161": {"username": "+-Moldelcy-Kulcacek-+", "id": 356867274248028161}, "569877945993134120": {"username": "sᴢʏᴍᴇx", "id": 569877945993134120}, "601758183521779732": {"username": "jauuuuuuuuuinteria.pl", "id": 601758183521779732}, "294778695120453633": {"username": "🎨 The Bob Ross Kid 🎨", "id": 294778695120453633}, "302050872383242240": {"username": "DISBOARD", "id": 302050872383242240}, "601364613275320340": {"username": "dean", "id": 601364613275320340}, "312594225239687169": {"username": "愛して", "id": 312594225239687169}, "221345446595919872": {"username": "Czareg", "id": 221345446595919872}, "354320941360349185": {"username": "Mały tygrysek", "id": 354320941360349185}, "213466096718708737": {"username": "Mantaro", "id": 213466096718708737}, "272414342094258177": {"username": "Aser", "id": 272414342094258177}, "172002275412279296": {"username": "Tatsumaki", "id": 172002275412279296}, "447781253786107905": {"username": "Pr3stiz0w4", "id": 447781253786107905}, "541050603237933078": {"username": "SznikoV", "id": 541050603237933078}, "294162383486713856": {"username": "Meivels", "id": 294162383486713856}, "529084894207279106": {"username": "Kitek", "id": 529084894207279106}, "400670388339867651": {"username": ".S o v e l 💞", "id": 400670388339867651}, "513004084954005504": {"username": "maurycy", "id": 513004084954005504}, "390177660220997632": {"username": "Angel~l.. ✝", "id": 390177660220997632}, "587335797657829383": {"username": "Crunchyskipper", "id": 587335797657829383}, "532983330552021014": {"username": "Bebokk", "id": 532983330552021014}, "327053235674415104": {"username": "Quler", "id": 327053235674415104}, "320560255870173195": {"username": "Hatmes", "id": 320560255870173195}, "296959452123234306": {"username": "Darke", "id": 296959452123234306}, "603111310770831360": {"username": "Web Panel", "id": 603111310770831360}, "512382618956857355": {"username": "Oracle", "id": 512382618956857355}, "382537106364563456": {"username": "spok", "id": 382537106364563456}, "282463924450885632": {"username": "MrMarshal", "id": 282463924450885632}, "392019896151834635": {"username": "Ня", "id": 392019896151834635}, "369206347226546177": {"username": "Kamelowy", "id": 369206347226546177},"234395307759108106": {"username": "Groovy", "id": 234395307759108106}, "407121583329771520": {"username": "lil rzabson", "id": 407121583329771520}, "411890159886270464": {"username": "danielek.", "id": 411890159886270464}, "331741160861073418": {"username": "Echo", "id": 331741160861073418}, "330819274275028995": {"username": "Yui", "id": 330819274275028995}, "229331513332727808": {"username": "elisia", "id": 229331513332727808}, "503299438757019659": {"username": "hkg", "id": 503299438757019659}, "251772462147043330": {"username": "Boogaś", "id": 251772462147043330}, "330650940195471361": {"username": "olsz4lvaro", "id": 330650940195471361}, "277086775854366720": {"username": "Flaku", "id": 277086775854366720}, "410890381270581248": {"username": "Pebel", "id": 410890381270581248}, "508284559515975690": {"username": "inshine", "id": 508284559515975690}, "524897662370316289": {"username": "Tup Tup", "id": 524897662370316289}, "423493776368074762": {"username": "Ambrozja", "id": 423493776368074762}, "531120461938163723": {"username": "MrMatrx.pl", "id": 531120461938163723}, "592438906016759829": {"username": "_Matys", "id": 592438906016759829}, "435309823484100619": {"username": "Szybet", "id": 435309823484100619}, "294936429115736064": {"username": "P5505PL 🦊", "id": 294936429115736064}, "543943963187347471": {"username": "kvsiol", "id": 543943963187347471}};
  
  let users = new Map();
  
  for(const id in usersObj) users.set(id, usersObj[id]);
  
  const mentions = ["<@525752440230772767>", "<@327885457150967809>", "<@265807190936387584>", "<@300171496897118221>", "<@195935967440404480>", "<@242730576195354624>", "<@219926422297247754>", "<@358254847872598027>", "<@329694416472375298>", "<@356867274248028161>", "<@569877945993134120>", "<@601758183521779732>", "<@294778695120453633>", "<@302050872383242240>", "<@601364613275320340>", "<@312594225239687169>", "<@221345446595919872>", "<@354320941360349185>", "<@213466096718708737>", "<@272414342094258177>", "<@172002275412279296>", "<@447781253786107905>", "<@541050603237933078>", "<@294162383486713856>", "<@529084894207279106>", "<@400670388339867651>", "<@513004084954005504>", "<@390177660220997632>", "<@587335797657829383>", "<@532983330552021014>", "<@327053235674415104>", "<@320560255870173195>", "<@296959452123234306>", "<@603111310770831360>", "<@512382618956857355>", "<@382537106364563456>", "<@282463924450885632>", "<@392019896151834635>", "<@369206347226546177>", "<@234395307759108106>", "<@407121583329771520>", "<@411890159886270464>", "<@331741160861073418>", "<@330819274275028995>", "<@229331513332727808>", "<@503299438757019659>", "<@251772462147043330>", "<@330650940195471361>", "<@277086775854366720>", "<@410890381270581248>", "<@508284559515975690>", "<@524897662370316289>", "<@423493776368074762>", "<@531120461938163723>", "<@592438906016759829>", "<@435309823484100619>", "<@294936429115736064>", "<@543943963187347471>"];
  
  
  function getUserFromMentionRegEx(mention) {
  	const matches = mention.match(/^<@!?(\d+)>$/);
  	if (!matches) return;
  	const id = matches[1];
  	return users.get(id);
  }
  
  function getUserFromMentionRegExWithLengthCheck(mention) {
  	const matches = mention.match(/^<@!?(\d{18})>$/);
  	if (!matches) return;
  	const id = matches[1];
  	return users.get(id);
  }
  
  function getUserFromMention(mention) {
  	if (!mention) return;
  	if (mention.startsWith('<@') && mention.endsWith('>')) {
  		mention = mention.slice(2, -1);
  		if (mention.startsWith('!')) {
  			mention = mention.slice(1);
  		}
  		return users.get(mention);
  	}
  }
  
  

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
regex
for(const mention of mentions)
{
    getUserFromMentionRegExWithLengthCheck(mention)
}
pending…
regex with length check
for(const mention of mentions)
{
    getUserFromMentionRegEx(mention)
}
pending…
default
for(const mention of mentions)
{
    getUserFromMention(mention)
}
pending…

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

Compare results of other browsers

0 Comments