| 647 | } |
| 648 | |
| 649 | sort() { |
| 650 | if (typeof this !== 'object' || this === null || !(#searchParams in this)) |
| 651 | throw new ERR_INVALID_THIS('URLSearchParams'); |
| 652 | |
| 653 | const a = this.#searchParams; |
| 654 | const len = a.length; |
| 655 | |
| 656 | if (len <= 2) { |
| 657 | // Nothing needs to be done. |
| 658 | } else if (len < 100) { |
| 659 | // 100 is found through testing. |
| 660 | // Simple stable in-place insertion sort |
| 661 | // Derived from v8/src/js/array.js |
| 662 | for (let i = 2; i < len; i += 2) { |
| 663 | const curKey = a[i]; |
| 664 | const curVal = a[i + 1]; |
| 665 | let j; |
| 666 | for (j = i - 2; j >= 0; j -= 2) { |
| 667 | if (a[j] > curKey) { |
| 668 | a[j + 2] = a[j]; |
| 669 | a[j + 3] = a[j + 1]; |
| 670 | } else { |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | a[j + 2] = curKey; |
| 675 | a[j + 3] = curVal; |
| 676 | } |
| 677 | } else { |
| 678 | // Bottom-up iterative stable merge sort |
| 679 | const lBuffer = new Array(len); |
| 680 | const rBuffer = new Array(len); |
| 681 | for (let step = 2; step < len; step *= 2) { |
| 682 | for (let start = 0; start < len - 2; start += 2 * step) { |
| 683 | const mid = start + step; |
| 684 | let end = mid + step; |
| 685 | end = end < len ? end : len; |
| 686 | if (mid > end) |
| 687 | continue; |
| 688 | merge(a, start, mid, end, lBuffer, rBuffer); |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (this.#context) { |
| 694 | setURLSearchParamsModified(this.#context); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | // https://heycam.github.io/webidl/#es-iterators |
| 699 | // Define entries here rather than [Symbol.iterator] as the function name |