diff --git a/js/src/builtin/Sorting.js b/js/src/builtin/Sorting.js index d0b3483c6d2a..0451ceb7a2fb 100644 --- a/js/src/builtin/Sorting.js +++ b/js/src/builtin/Sorting.js @@ -230,11 +230,6 @@ function MoveHoles(sparse, sparseLen, dense, denseLen) { // Iterative, bottom up, mergesort. function MergeSort(array, len, comparefn) { - // To save effort we will do all of our work on a dense list, - // then create holes at the end. - var denseList = new List(); - var denseLen = 0; - // Until recently typed arrays had no sort method. To work around that // many users passed them to Array.prototype.sort. Now that we have a // typed array specific sorting method it makes sense to divert to it @@ -243,6 +238,11 @@ function MergeSort(array, len, comparefn) { return callFunction(TypedArraySort, array, comparefn); } + // To save effort we will do all of our work on a dense list, + // then create holes at the end. + var denseList = new List(); + var denseLen = 0; + for (var i = 0; i < len; i++) { if (i in array) denseList[denseLen++] = array[i]; @@ -253,7 +253,7 @@ function MergeSort(array, len, comparefn) { // Insertion sort for small arrays, where "small" is defined by performance // testing. - if (len < 24) { + if (denseLen < 24) { InsertionSort(denseList, 0, denseLen - 1, comparefn); MoveHoles(array, len, denseList, denseLen); return array;