Cast vs Math int to frac

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function cnvrt3 (n) {
      return n / Math.pow(10,(""+n).length)
    }
   
    function cvt3(n) {
      return n / Math.pow(10, Math.ceil(Math.log(n)/Math.LN10));
    }
   
    function cnvrt1 (integerPart) {
        var fractionalPart = integerPart;
   
        while(fractionalPart > 1) {
            fractionalPart = fractionalPart / 10;
        }
        return fractionalPart;
    };
   
    cnvrt2 = function(integerPart) {
        return parseFloat('.' + integerPart);
    };
   
    function ab(n) {
            var a = 566123;
   
            var len = a.toString().length;
            var decimalPoints = 1;
   
            for (var i = 0; i < len; i++) {
                decimalPoints = decimalPoints * 10;
            }
   
            var number = parseInt(a) / decimalPoints;
            alert(number);
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Math
cvt3(132232);
pending…
Cast
cnvrt3(132232);
pending…
convertIntegerPartToFractionalPart1
cnvrt1(132232);
pending…
convertIntegerPartToFractionalPart2
cnvrt2(132232)
pending…
Mota Chuha
ab(132232)
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment