On Thu, Mar 17, 2016 at 7:21 PM, Bergi <[email protected]> wrote: > Isiah Meadows wrote: >> A polyfill could check if `A.p.sort` is stable, replacing if necessary, >> and >> alias the old one to `A.p.fastSort` if it doesn't exist. > > How does one check/test for the stability of a black-box sort? You can't, > afaik. > > In my opinion, you'll never be able to rely on the stability of > `Array.prototype.sort` because of backward-compatibility with older > implementations where it is unstable. > > As ugly as it might be, I'd recommend a separate > `Array.protytype.stableSort` (with an unambiguous name) therefore that can > be tested for existence and polyfilled in absence. > Or at least we'd need to tag the available implementations explicitly: > ```js > Array.prototype.fastSort[Symbol.isStable] = false; > Array.prototype.stableSort[Symbol.isStable] = true; > Array.prototype.sort[Symbol.isStable] = /* to be chosen by implementer */; > ``` > which has the benefit that a test `if (…sort[Symbol.isStable])` will yield a > falsy default value (`undefined`) in legacy implementations.
If you're planning on pessimistically assuming that legacy implementations use an unstable sort for Array#sort(), then testing for the presence of Array#fastSort() (and assuming that when it appears the Array#sort is stable) is exactly as useful as testing for the presence of Array#stableSort (and assuming that when it appears the Array#sort is unstable/fast). So, polyfilling isn't a reason to prefer one default vs the other. ~TJ _______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

