Inserting into a sorted array
JavaScript performance comparison
Preparation code
<script>
function getRandom() {
return Math.floor(Math.random() * 10000);
}
var findLocationLogarithmic = function(n, arr) {
var left = 0;
var right = arr.length  1;
while (left <= right){
var mid = Math.floor((left + right) / 2);
if (arr[mid] == n)
return mid;
else if (arr[mid] < n)
left = mid + 1;
else
right = mid  1;
}
return arr.length;
}
var findLocationLinear = function(n, arr) {
var i, len = arr.length;
for (i = 0; i < len; i++) {
if (arr[i] >= n) return i;
}
return len;
}
</script>
<script>
Benchmark.prototype.setup = function() {
var largeArray = [];
for (var n = 0; n < 10000; n++) {
largeArray.push(n);
}
};
Benchmark.prototype.teardown = function() {
delete largeArray;
};
</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… 
splice() with logarithmic search 

pending… 
splice() with linear 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
 Revision 25: published by artem
 Revision 26: published by artem
 Revision 27: published by artem
0 comments
Comment form temporarily disabled.