Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs For loop [1-9]|X 2
(version: 0)
Comparing performance of:
Regex vs For loop, ParseInt vs For loop with switch case vs For loop, toString
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var dataPool = ["1234", "1X34", "0123", "12x4"]; var testStr = dataPool[Math.floor(Math.random() * dataPool.length)];
Tests:
Regex
let isValid = true; let regStr = `(${i+1}|X)`; const regEx = new RegExp(regStr); for (let i = 0; i < testStr.length; i++) { let code = testStr.charAt(i); if(regEx.test(code) === false){ isValid = false; break; } } return isValid;
For loop, ParseInt
let isValid = true; for (let i = 0; i < testStr.length; i++) { let code = testStr.charAt(i); if (code === "X"){ } else{ let codeValue = parseInt(code, 10); if(codeValue !== i+1){ isValid = false; break; } } } return isValid;
For loop with switch case
let isValid = true; for (let i = 0; i < testStr.length; i++) { let char = testStr.charAt(i); switch (true) { case char === 'X': break; case parseInt(char, 10) === i + 1: break; default: isValid = false; } } return isValid;
For loop, toString
let isValid = true; for (let i = 0; i < testStr.length; i++) { let code = testStr.charAt(i); if (code === "X"){ } else{ if(code !== (i+1).toString()){ isValid = false; break; } } } return isValid;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Regex
For loop, ParseInt
For loop with switch case
For loop, toString
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
Three Digit Validator 2
Three Digit Validator 3
Regex vs For loop [1-9]|X
DTMF: array includes vs regex
Comments
Confirm delete:
Do you really want to delete benchmark?