|
187 | 187 | __chakraLibrary.arrayCreateDataPropertyOrThrow(compareArray, index, "" + array[index]); |
188 | 188 | } |
189 | 189 |
|
190 | | - const fast = length < 1073741825; |
191 | 190 | const buffer = []; |
192 | 191 | const stringBuffer = []; |
193 | | - |
194 | | - let i = 1, j = 0, k = 0, bucketSize = 4, position = 0; |
| 192 | + let i = 1, j = 0, k = 0, bucketSize = 4, lastSize = 2, position = 0; |
195 | 193 |
|
196 | 194 | // handle pairs first |
197 | 195 | while (i < length) { |
|
212 | 210 | while (position < length) { |
213 | 211 | const left = position; |
214 | 212 | let right = position + bucketSize; |
215 | | - right < length ? right : length; |
216 | | - const mid = fast ? left + right >> 1 : (left + right) / 2 |0; |
| 213 | + const mid = left + lastSize; |
217 | 214 |
|
218 | 215 | // perform a merge but only if it's necessary |
219 | | - if (mid < length && compareArray[mid - 1] > compareArray[mid]) { |
| 216 | + if (mid < length && compareArray[mid] < compareArray[mid - 1]) { |
| 217 | + right = right < length ? right : length; |
220 | 218 | i = left, j = 0, k = left; |
221 | 219 | const currentLength = buffer.length; |
222 | 220 |
|
|
269 | 267 | position += bucketSize; |
270 | 268 | } |
271 | 269 | position = 0; |
| 270 | + lastSize = bucketSize; |
272 | 271 | bucketSize *= 2; |
273 | 272 | } |
274 | 273 | }); |
275 | 274 |
|
276 | 275 | platform.registerChakraLibraryFunction("MergeSort", function(array, length, compareFn) { |
277 | | - const fast = length < 1073741825; |
278 | 276 | const buffer = []; |
279 | | - let i = 1, j = 0, k = 0, bucketSize = 4, position = 0; |
| 277 | + let i = 1, j = 0, k = 0, bucketSize = 4, lastSize = 2, position = 0; |
280 | 278 |
|
281 | 279 | // handle pairs first |
282 | 280 | while (i < length) { |
|
294 | 292 | while (position < length) { |
295 | 293 | const left = position; |
296 | 294 | let right = position + bucketSize; |
297 | | - right < length ? right : length; |
298 | | - const mid = fast ? left + right >> 1 : (left + right) / 2 |0; |
| 295 | + const mid = left + lastSize; |
299 | 296 |
|
300 | 297 | // perform a merge but only if it's necessary |
301 | | - if (mid < length && compareFn(array[mid - 1], array[mid]) > 0) { |
| 298 | + if (mid < length && compareFn(array[mid], array[mid - 1]) < 0) { |
| 299 | + right = right < length ? right : length; |
302 | 300 | i = left, j = 0, k = left; |
303 | 301 | const currentLength = buffer.length; |
304 | 302 |
|
|
342 | 340 | position += bucketSize; |
343 | 341 | } |
344 | 342 | position = 0; |
| 343 | + lastSize = bucketSize; |
345 | 344 | bucketSize *= 2; |
346 | 345 | } |
347 | 346 | }); |
|
376 | 375 | __chakraLibrary.raiseFunctionArgument_NeedFunction("Array.prototype.sort"); |
377 | 376 | } |
378 | 377 |
|
379 | | - const objInfo = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.sort"); |
380 | | - const array = objInfo.o; |
381 | | - let len = objInfo.len; |
| 378 | + const {o, len} = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.sort"); |
382 | 379 |
|
383 | 380 | if (len < 2) { // early return if length < 2 |
384 | | - return array; |
| 381 | + return o; |
385 | 382 | } |
386 | 383 |
|
387 | 384 | // check for if the array has any missing values |
388 | 385 | // also pull in any values from the prototype |
389 | | - let i = 0; |
| 386 | + let i = 0, length = len; |
390 | 387 | while (i < len) { |
391 | | - if (array[i] === undefined) { |
392 | | - len = __chakraLibrary.FillArrayHoles(array, len, i); |
| 388 | + if (o[i] === undefined) { |
| 389 | + length = __chakraLibrary.FillArrayHoles(o, len, i); |
393 | 390 | break; |
394 | 391 | } |
395 | | - array[i] = array[i++]; |
| 392 | + o[i] = o[i++]; |
396 | 393 | } |
397 | 394 |
|
398 | | - if (len > 1) |
| 395 | + if (length > 1) |
399 | 396 | { |
400 | 397 | if (compareFn !== undefined) { |
401 | | - __chakraLibrary.MergeSort(array, len, compareFn); |
| 398 | + __chakraLibrary.MergeSort(o, length, compareFn); |
402 | 399 | } else { |
403 | | - __chakraLibrary.DefaultMergeSort(array, len); |
| 400 | + __chakraLibrary.DefaultMergeSort(o, length); |
404 | 401 | } |
405 | 402 | } |
406 | 403 |
|
407 | | - return array; |
| 404 | + return o; |
408 | 405 | }); |
409 | 406 |
|
410 | 407 | platform.registerFunction(platform.FunctionKind.Array_filter, function (callbackfn, thisArg = undefined) { |
|
0 commit comments