(fn)
| 56 | * @returns {ParameterizedComparator<TArg, T>} comparator |
| 57 | */ |
| 58 | const createCachedParameterizedComparator = (fn) => { |
| 59 | /** @type {WeakMap<TArg, Comparator<T>>} */ |
| 60 | const map = new WeakMap(); |
| 61 | return (arg) => { |
| 62 | const cachedResult = map.get(arg); |
| 63 | if (cachedResult !== undefined) return cachedResult; |
| 64 | // arrow closure dispatches faster than a bound function on the sort hot path |
| 65 | /** |
| 66 | * @param {T} a first item |
| 67 | * @param {T} b second item |
| 68 | * @returns {-1 | 0 | 1} compare result |
| 69 | */ |
| 70 | const result = (a, b) => fn(arg, a, b); |
| 71 | map.set(arg, result); |
| 72 | return result; |
| 73 | }; |
| 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * Compares the provided values and returns their ordering. |
no test coverage detected