Maintaining a sorted array vs. push() and sort()
JavaScript performance comparison
Info
The actual usecase isn't to sort for every insertion. Either you keep a sorted array, or you sort it at the end.
I use linear search as a baseline.
Preparation code
<script>
function findBSearch(n, array) {
var low = 0, high = array.length;
while (low < high) {
var mid = (low + high) >>> 1;
array[mid] < n ? low = mid + 1 : high = mid;
}
return low;
};
function findIndexOf(el, arr) {
for (var i = 0; arr[i] < el; i++) {}
return i;
}
</script>
<script>
Benchmark.prototype.setup = function() {
// Pseudorandom data.
// I keep it the same for the sake of comparison.
var input = [];
for(i=0;i<300000;i++) input.push(Math.floor(Math.random()));
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test  Ops/sec  

push() and sort() 

pending… 
Linear search 

pending… 
Binary search 

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:
 Revision 1: published
 Revision 2: published and last updated
 Revision 3: published
 Revision 4: published by espadrine
 Revision 5: published
 Revision 6: published
 Revision 7: published
 Revision 8: published
 Revision 9: published
 Revision 21: published
 Revision 22: published
 Revision 23: published
 Revision 24: published
1 comment
Ben Adams commented : ∞
Broken test in very many ways (check upper bound on linear search for loop as example)
Improved tests in version 22 http://jsperf.com/insertintosorted/22