Created
October 12, 2022 21:39
-
-
Save BobbyWibowo/9174e51541742cb21b0add04f12c6b92 to your computer and use it in GitHub Desktop.
regex.test vs known string.startsWith
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"title":"regex.test vs known string.startsWith","initialization":"const arr = [];\nconst prefixes = ['startswith', 'emojis', 'custom', null];\n\nfor (let i = 0, j = 100; i < j; i++) {\n const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];\n const id = Math.round(Math.random() * 999999999);\n arr.push({\n id: prefix\n ? `${prefix}-${id}`\n : id\n });\n}\n\nconst regex = /^(startswith|emojis|custom)-/;\n\nconst funct = id => {\n if (typeof id !== 'string') return false;\n return id.startsWith('startswith-') ||\n id.startsWith('emojis-') ||\n id.startsWith('custom-');\n};\n\nconst expected = arr.reduce((acc, curr) => {\n return funct(curr.id) ? (acc + 1) : acc;\n}, 0);\n\nconst check = count => {\n if (count !== expected) {\n throw new Error('Final count mismatch');\n }\n}","setup":"// runs before each test","tests":[{"name":"test 1","code":"let found = 0;\nfor (let i = 0; i < arr.length; i++) {\n if (regex.test(arr[i].id)) found++;\n}\ncheck(found);","results":{"aborted":false,"count":11408,"cycles":5,"hz":137383.2537674571,"stats":{"moe":1.2823126600884092e-7,"rme":1.7616828559014888,"sem":6.542411531063313e-8,"deviation":4.895892486503611e-7,"mean":0.000007278907527497189,"variance":2.3969763239402514e-13,"numSamples":56},"times":{"cycle":0.08303777707368792,"elapsed":7.434,"period":0.000007278907527497189,"timeStamp":1665610734786}},"platforms":{"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0":{"aborted":false,"count":11408,"cycles":5,"hz":137383.2537674571,"stats":{"moe":1.2823126600884092e-7,"rme":1.7616828559014888,"sem":6.542411531063313e-8,"deviation":4.895892486503611e-7,"mean":0.000007278907527497189,"variance":2.3969763239402514e-13,"numSamples":56},"times":{"cycle":0.08303777707368792,"elapsed":7.434,"period":0.000007278907527497189,"timeStamp":1665610734786}}}},{"name":"test 2","code":"let found = 0;\nfor (let i = 0; i < arr.length; i++) {\n if (funct(arr[i].id)) found++;\n}\ncheck(found);","results":{"aborted":false,"count":101577,"cycles":4,"hz":1209238.6975519592,"stats":{"moe":1.2017994694388791e-8,"rme":1.4532624251429058,"sem":6.1316299461167305e-9,"deviation":4.6292790911529426e-8,"mean":8.2696658817192e-7,"variance":2.1430224903785813e-15,"numSamples":57},"times":{"cycle":0.08400078512673911,"elapsed":7.148,"period":8.2696658817192e-7,"timeStamp":1665610742227}},"platforms":{"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0":{"aborted":false,"count":101577,"cycles":4,"hz":1209238.6975519592,"stats":{"moe":1.2017994694388791e-8,"rme":1.4532624251429058,"sem":6.1316299461167305e-9,"deviation":4.6292790911529426e-8,"mean":8.2696658817192e-7,"variance":2.1430224903785813e-15,"numSamples":57},"times":{"cycle":0.08400078512673911,"elapsed":7.148,"period":8.2696658817192e-7,"timeStamp":1665610742227}}}}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment