Formatting Currency (RegExp vs. Looping) (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script>
  var total = 100000000;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Loop
var dollars = (total / 100).toFixed(2);
for (var i = 0; i < Math.floor((dollars.length - (4 + i)) / 3); i++) {
 var offset = dollars.length - 3 - (4 * i + 3);
 dollars = dollars.substring(0, offset) + ',' + dollars.substring(offset);
}
ready
Regular Expression
var dollars = (total / 100).toFixed(2);
var regexp = new RegExp('([0-9]+)([0-9]{3})');
while (regexp.test(dollars)) {
 dollars = dollars.replace(regexp, '$1,$2');
}
ready

Revisions

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