64bit compare

JavaScript performance comparison

Test case created

Preparation code

<script type="text/javascript">

var i, arr = []; // array of string numbers

function compare(a,b){
    var aa = [0,0,0], ba = [0,0,0], i = 3;
    while (a.length) aa[--i] = parseInt(a.slice(-9)), a = a.slice(0,-9);
    i = 3;
    while (b.length) ba[--i] = parseInt(b.slice(-9)), b = b.slice(0,-9);
    if (aa[0] === ba[0]){
        if (aa[1] === ba[1]) {
            if (aa[2] === ba[2]) return 0;
            else if (aa[2] > ba[2]) return 1;
            return -1;
        } else if (aa[1] > ba[1]) return 1;
        return -1;
    } else if (aa[0] > ba[0]) return 1;
    return -1;
}

function pad(str, len) { // Your fav. padding fn
    var pre = '0';
    len = len - str.length;
    while (len > 0) {
        if (len & 1) str = pre + str;
        len >>= 1;
        pre += pre;
    }
    return str;
};

</script>
<script>
Benchmark.prototype.setup = function() {
    arr = [];
    while ( arr.length < 10 ) arr[arr.length] = Math.random().toString().slice(2);
};
</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
split
i = 10;
while (--i > 0) compare(arr[i], arr[i-1]);
pending…
pad
i = 10;
while (--i > 0) pad(arr[i],20) < pad(arr[i-1],20);
pending…

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

Compare results of other browsers

0 comments

Add a comment