Skip to content

Commit

Permalink
minimize heap allocations for maxSatisfying/minSatisfying
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat authored and isaacs committed Feb 2, 2017
1 parent 85ce654 commit 32802c5
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions semver.js
Expand Up @@ -1087,20 +1087,34 @@ function satisfies(version, range, loose) {

exports.maxSatisfying = maxSatisfying;
function maxSatisfying(versions, range, loose) {
return versions.filter(function(version) {
return satisfies(version, range, loose);
}).sort(function(a, b) {
return rcompare(a, b, loose);
})[0] || null;
var max = null;
var maxSV = null;
var rangeObj = new Range(range, loose);
versions.forEach(function (v) {
if (rangeObj.test(v)) { // satisfies(v, range, loose)
if (!max || maxSV.compare(v) === -1) { // compare(max, v, true)
max = v;
maxSV = new SemVer(max, loose);
}
}
})
return max;
}

exports.minSatisfying = minSatisfying;
function minSatisfying(versions, range, loose) {
return versions.filter(function(version) {
return satisfies(version, range, loose);
}).sort(function(a, b) {
return compare(a, b, loose);
})[0] || null;
var min = null;
var minSV = null;
var rangeObj = new Range(range, loose);
versions.forEach(function (v) {
if (rangeObj.test(v)) { // satisfies(v, range, loose)
if (!min || minSV.compare(v) === 1) { // compare(min, v, true)
min = v;
minSV = new SemVer(min, loose);
}
}
})
return min;
}

exports.validRange = validRange;
Expand Down

0 comments on commit 32802c5

Please sign in to comment.