ealks

JavaScript performance comparison

Test case created

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
rosetta
function merge(left,right,arr){
        var a=0;
        while(left.length&&right.length)
                arr[a++]=right[0]<left[0]?right.shift():left.shift();
        while(left.length)arr[a++]=left.shift();
        while(right.length)arr[a++]=right.shift();
}
function mSort(arr,tmp,l){
        if(l==1)return;
        var     m=Math.floor(l/2),
                tmp_l=tmp.slice(0,m),
                tmp_r=tmp.slice(m);
        mSort(tmp_l,arr.slice(0,m),m);
        mSort(tmp_r,arr.slice(m),l-m);
        merge(tmp_l,tmp_r,arr);
}
function merge_sort(arr){
        mSort(arr,arr.slice(),arr.length);
}
 
var arr=[1,5,2,7,3,9,4,6,8];
merge_sort(arr); // arr will now: 1,2,3,4,5,6,7,8,9
pending…
mine
var mSort = function(a, b, start, end) {
  var mid;

  if(start < end) {
    mid = (start + end)/2 | 0;
    mSort(a, b, start, mid);
    mSort(a, b, mid+1, end);
    merge(a, b, start, mid, end);
  }
};

var merge = function(a, b, start, mid, end) {
  var left = start;
  var right = mid + 1;
  var i = start;

  while(left <= mid && right <= end) {
    if(a[left] <= a[right]) {
      b[i++] = a[left++];
    } else {
      b[i++] = a[right++];
    }
  }

  while(left <= mid) {
    b[i++] = a[left++];
  }
  while(right <= end) {
    b[i++] = a[right++];
  }

  for(var j = start; j <= end; j++) {
    a[j] = b[j];
  }
};

var mergeSort = function(arr) {
  return mSort(arr, [], 0, arr.length-1);
};

var arr = [1,5,2,7,3,9,4,6,8];
mergeSort(arr);
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