ELO snippet formatting + performance

Not assigning `arr.length` to a variable in a for loop means that the read is done on every iteration.
This commit is contained in:
Talasan Nicholson
2018-05-30 11:07:10 -05:00
committed by GitHub
parent e93869b72a
commit b8695263ee

View File

@ -15,17 +15,20 @@ const elo = ([...ratings], kFactor = 32, selfRating) => {
const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400)); const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400));
const newRating = (rating, i) => const newRating = (rating, i) =>
(selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a)); (selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a));
if (ratings.length === 2) { if (ratings.length === 2) {
return [newRating(a, 1), newRating(b, 0)]; return [newRating(a, 1), newRating(b, 0)];
} else { }
for (let i = 0; i < ratings.length; i++) {
let j = i; for (let i = 0, l = ratings.length; i < l; i++) {
while (j < ratings.length - 1) { let j = i;
[ratings[i], ratings[j + 1]] = elo([ratings[i], ratings[j + 1]], kFactor);
j++; while (j < l - 1) {
} [ratings[i], ratings[j + 1]] = elo([ratings[i], ratings[j + 1]], kFactor);
j++;
} }
} }
return ratings; return ratings;
}; };
``` ```