Replace-replace-replace

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function replace3x(value) {
   value.toExponential().replace('.','').replace('-','').replace(/^0*/,'');
  }
  
  function regexreplace(value) {
   value.toExponential().replace(/(^\-?0*|\.0*)/g,'');
  }
  
  function opDigits(value) {
    return value
      .toExponential()
      .replace(/e[\+\-0-9]*$/, '') // remove exponential notation
    .replace(/^0\.0*|\./, '') // remove decimal point and leading zeros
    .length
  };
  
  var log10 = Math.log(10);
  
  function megawacDigits(n) {
    n = Math.abs(String(n).replace(".", "")); //remove decimal and make positive
    if (n == 0) return 0;
    while (n != 0 && n % 10 == 0) n /= 10; //kill the 0s at the end of n
  
    return Math.floor(Math.log(n) / log10) + 1; //get number of digits
  }
  
  function omerDigits(value) {
    return value
      .toExponential()
      .replace(/^([0-9]+)\.?([0-9]+)?e[\+\-0-9]*$/g, "$1$2")
      .length
  };

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Replace Many
replace3x(-0.00205);
replace3x(2.34);
replace3x(0.0034);
replace3x(3.141592653589793);
pending…
Replace once
regexreplace(-0.00205);
regexreplace(2.34);
regexreplace(0.0034);
regexreplace(3.141592653589793);
pending…
opDigits
opDigits(-0.00205)
opDigits(2.34);
opDigits(0.0034);
opDigits(3.141592653589793);
pending…
omerDigits
omerDigits(-0.00205);
omerDigits(2.34);
omerDigits(0.0034);
omerDigits(3.141592653589793);
pending…
megawacDigits
megawacDigits(-0.00205);
megawacDigits(2.34);
megawacDigits(0.0034);
megawacDigits(3.141592653589793);
pending…

Revisions

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

0 Comments