New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace lodash 'sortBy' usage with Array.prototype.sort #11810
Replace lodash 'sortBy' usage with Array.prototype.sort #11810
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 4f5673b:
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/32439/ |
Co-authored-by: Corey Farrell <git@cfware.com>
Still puzzling. One possibility is that the sort order may be reversed at the moment, based on some unit test failures where the received statements are flipped around from the expected statements:
I'm just doing some sorting via the node shell with |
Yep, the current > sortBy = require('lodash/sortBy')
[Function]
> arr = [{"a": 1}, {"a": -1}, {"a": 3}]
[ { a: 1 }, { a: -1 }, { a: 3 } ]
> sortFn = x => x.a * -1;
[Function: sortFn]
> sortBy(arr, sortFn)
[ { a: 3 }, { a: 1 }, { a: -1 } ]
> arr.sort((a, b) => sortFn(b) - sortFn(a))
[ { a: -1 }, { a: 1 }, { a: 3 } ]
> |
Thanks for investigating! |
I'm a bit puzzled about why the latest Travis checks are failing here for Node < v12. There must be a language feature in use that's only supported in later versions? |
Apparently The following code demonstrates this problem: const items = [];
for (let i = 0; i < 20; i++) {
items.push({idx: i, value: 'a'});
}
const original = process.versions.node.split('.')[0] < 12 ? new Map(items.map((v, idx) => [v, idx])) : undefined;
items[10].value = 'b';
items.sort((a, b) => {
if (a.value < b.value) {
return -1;
}
if (a.value > b.value) {
return 1;
}
// This if statement makes the sort stable for node.js < 12, uses a Map to find original indexes.
if (original) {
return original.get(a) - original.get(b);
}
return 0;
});
console.log(items); |
Well, today's turning into a good day for learning software archaeology; thank you @coreyfarrell :) |
@jayaddison I just did some more experimentation with my sort stability demo, found an easier way to get it working for older node. See my previous message I've edited the example code to use a |
@coreyfarrell Nice! Just to rephrase it as I understand it: for Node versions prior to v12, we store a map keyed on each node object and storing the array index for that object as a value. If that map exists during sorting, then it's used as a tie-breaker to ensure stable sort behaviour. Nice approach. I'll start incorporating that here. |
(we might also want to keep the 'Sorting with map' section from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Browser_compatibility in mind, particularly if any of the performance concerns mentioned there crop up) |
…original element indices
Co-authored-by: Corey Farrell <git@cfware.com>
…e is strictly true/false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems good to me. Only remaining question does the access to process.versions
need to guard against running outside Node.js? Maybe (typeof process !== 'undefined' ? process.version.node.split(".")[0] : 0)
?
…ironments Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
…ind since no return value is required Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
…lent map expression Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
: undefined; | ||
const stabilityMap = | ||
!nodeVersion || nodeVersion < 12 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
: undefined; | |
const stabilityMap = | |
!nodeVersion || nodeVersion < 12 | |
: 0; | |
const stabilityMap = | |
nodeVersion < 12 |
Feel free to ignore this suggestion, just wanted to point out that the conditional can be simplified if you make browsers produce nodeVersion === 0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @coreyfarrell - I think I'd prefer to keep the meaning/semantics of nodeVersion
closer to the ground truth (i.e. it'll genuinely have an undefined value in non-Node environments), even though I'd admit that it's (hopefully) unlikely that anyone would be line-by-line debugging the value. I'm sure I've probably used tricks like this to simplify my own code elsewhere too, so it's a bit arbitrary of me not to accept. But I think I'd prefer the more verbose condition in this case.
…n equivalent map expression" This reverts commit 8cc9106.
…an invalid version string..)
NB: There's a change to the babel/packages/babel-core/src/transformation/file/file.js Lines 141 to 158 in 5bbad89
|
(closing and cleaning up a few stale pull requests; see #11789 (comment) and subsequent comments in case it's useful to re-open from github pr refs at a later date) |
This is likely a lower-priority change now that
lodash
4.17.19 has been released and resolves a vulnerability that caused package audit warnings and noise for many users; that was the original motivation for removing some of these functions as per #11789.That said, there still seems to be scope for further dependency reduction, so this changeset is an attempt to remove one more
lodash
reference.