Oooo Boy!

Benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
 var phoneFormat = "(###) ###-####";
 var phone = "1234567890";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Anthony
phoneArray = phone.split("");
var formatted = $.map(phoneFormat, function(ch) {
 if (ch == "#") {
  return phoneArray.shift();
 } else {
  return ch;
 }
});
formatted.join("");
ready
Dave
var tmpString = "";
var numPos = phone.length - 1;
for (var i = phoneFormat.length - 1; i >= 0; i--) {
 if (phoneFormat[i] == "#" && numPos >= 0) {
  tmpString = phone[numPos] + tmpString;
  numPos--;
 } else if (phoneFormat[i] != "#" && numPos >= 0) {
  tmpString = phoneFormat[i] + tmpString;
 }
 for (var i = phoneFormat.length - 1; i >= 0; i--) {
  if (phoneFormat[i] == "#" && numPos >= 0) {
   tmpString = phone[numPos] + tmpString;
   numPos--;
  } else if (phoneFormat[i] != "#" && numPos >= 0) {
   tmpString = phoneFormat[i] + tmpString;
  }
  for (var i = phoneFormat.length - 1; i >= 0; i--) {
   if (phoneFormat[i] == "#" && numPos >= 0) {
    tmpString = phone[numPos] + tmpString;
    numPos--;
   } else if (phoneFormat[i] != "#" && numPos >= 0) {
    tmpString = phoneFormat[i] + tmpString;
   }
   for (var i = phoneFormat.length - 1; i >= 0; i--) {
    if (phoneFormat[i] == "#" && numPos >= 0) {
     tmpString = phone[numPos] + tmpString;
     numPos--;
    } else if (phoneFormat[i] != "#" && numPos >= 0) {
     tmpString = phoneFormat[i] + tmpString;
    }
    for (var i = phoneFormat.length - 1; i >= 0; i--) {
     if (phoneFormat[i] == "#" && numPos >= 0) {
      tmpString = phone[numPos] + tmpString;
      numPos--;
     } else if (phoneFormat[i] != "#" && numPos >= 0) {
      tmpString = phoneFormat[i] + tmpString;
     }
    };
   };
  };
 };
};
ready

Revisions

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