Native sort vs Insertion sort

JavaScript performance comparison

Test case created by Native sort vs Insertion sort

Preparation code

<script>
  Array.prototype.inSort = function(fn) {
    var i, n, j, key;
    for (i = 1, n = this.length; i < n; i++) {
      key = this[i]
      j = i - 1;

      while (j >= 0 && fn ? fn(this[j], key) > 0 : this[j] > key) {
        this[j + 1] = this[j]
        j = j - 1
      }

      this[j + 1] = key
    }
    return this;
  };
</script>
<script>
Benchmark.prototype.setup = function() {
    var data = [{
      name: 'C',
      value: 3
    }, {
      name: 'D',
      value: 4
    }, {
      name: 'K',
      value: 11
    }, {
      name: 'L',
      value: 12
    }, {
      name: 'A',
      value: 1
    }, {
      name: 'B',
      value: 2
    },
   
    {
      name: 'M',
      value: 13
    }, {
      name: 'N',
      value: 14
    }, {
      name: 'E',
      value: 5
    }, {
      name: 'F',
      value: 6
    }, {
      name: 'G',
      value: 7
    }, {
      name: 'H',
      value: 8
    }, {
      name: 'I',
      value: 9
    }, {
      name: 'J',
      value: 10
    },
   
   
    {
      name: 'O',
      value: 15
    }, {
      name: 'P',
      value: 16
    }, {
      name: 'S',
      value: 19
    }, {
      name: 'T',
      value: 20
    }, {
      name: 'U',
      value: 21
    }, {
      name: 'V',
      value: 22
    }, {
      name: 'W',
      value: 23
    }, {
      name: 'Q',
      value: 17
    }, {
      name: 'R',
      value: 18
    },
   
    {
      name: 'X',
      value: 24
    }, {
      name: 'Y',
      value: 25
    }, {
      name: 'Z',
      value: 26
    }];
};

Benchmark.prototype.teardown = function() {
    delete data;
};
</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
Native sort; return 0
data.sort(function() {
  return 0;
})
pending…
inSort; return 0
data.inSort(function() {
  return 0;
})
pending…
Native sort
data.sort(function(a, b) {
  return a.value - b.value
})
pending…
inSort
data.inSort(function(a, b) {
  return a.value - b.value
})
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