Inserting into a sorted array
JavaScript performance comparison
Preparation code
<script>
var findLocationFor = function(n, arr) {
var i = Math.floor(arr.length / 2);
var prev_i = arr.length;
while (1) {
var b = i;
if (arr[i] === n) return i + 1;
if (arr[i] < n) {
if (i === arr.length  1  arr[i + 1] > n) {
return i + 1;
}
i += Math.floor(Math.abs(prev_i  i) / 2);
} else // arr[i] > n
{
if (i === 0  arr[i  1] < n) {
return i + 1;
}
i = Math.floor(Math.abs(prev_i  i) / 2);
}
prev_i = b;
}
}
</script>
<script>
Benchmark.prototype.setup = function() {
var largeArray = [];
for (var n = 0; n < 10000; n++) {
largeArray.push(n);
}
var newVal = 1337.5;
};
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() 

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
0 comments