regex vs for loop (v19)

Revision 19 of this benchmark created by Thomas DeFrank on


Preparation HTML

<script>

function regex(num) {
   return num.toString().replace(/[13579](?=[13579])/g, "$&-");
}

function forloop(num) {
   num = num.toString()
   newstring = ''
   for ( i = 0; i < num.length; i++ ){
		if(num[i] % 2 !== 0 && num[i+1] % 2 !== 0){
			newstring += num[i] + '-'
		} else {
			newstring += num[i]
		}
   }
   
   if (newstring[newstring.length - 1] === '-'){
   	return newstring.slice(0, -1)
   }
   
   return newstring
}  
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
regex
regex(454793)
regex(123456)
regex(1003567)
ready
forloop
forloop(454793)
forloop(123456)
forloop(1003567)
ready

Revisions

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