Skip to content

Commit 407adc9

Browse files
ptomatoMs2ger
authored andcommitted
Small optimization in rounding
Instead of doing a multiplication here, we can just check if the remainder is zero. This matters a bit more for TimeDuration and RoundNumberToIncrementAsIfPositive, because we avoid a BigInt multiplication.
1 parent 686affb commit 407adc9

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

polyfill/lib/ecmascript.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4022,8 +4022,7 @@ export function RoundNumberToIncrement(quantity, increment, mode) {
40224022
const cmp = ComparisonResult(MathAbs(remainder * 2) - increment);
40234023
const even = r1 % 2 === 0;
40244024
const unsignedRoundingMode = GetUnsignedRoundingMode(mode, sign);
4025-
const rounded =
4026-
MathAbs(quantity) === r1 * increment ? r1 : ApplyUnsignedRoundingMode(r1, r2, cmp, even, unsignedRoundingMode);
4025+
const rounded = remainder === 0 ? r1 : ApplyUnsignedRoundingMode(r1, r2, cmp, even, unsignedRoundingMode);
40274026
return increment * (sign === 'positive' ? rounded : -rounded);
40284027
}
40294028

@@ -4042,7 +4041,7 @@ export function RoundNumberToIncrementAsIfPositive(quantity, increment, mode) {
40424041
// extra sign to make sure we treat it as positive
40434042
const cmp = remainder.times(2).abs().compare(increment) * (quantity.lt(0) ? -1 : 1);
40444043
const even = r1.isEven();
4045-
const rounded = quotient.times(increment).eq(quantity)
4044+
const rounded = remainder.isZero()
40464045
? quotient
40474046
: ApplyUnsignedRoundingMode(r1, r2, cmp, even, unsignedRoundingMode);
40484047
return rounded.times(increment);

polyfill/lib/timeduration.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class TimeDuration {
120120
const cmp = remainder.multiply(2).abs().compare(increment);
121121
const even = quotient.isEven();
122122
const unsignedRoundingMode = GetUnsignedRoundingMode(mode, sign);
123-
const rounded = this.totalNs.abs().eq(r1) ? r1 : ApplyUnsignedRoundingMode(r1, r2, cmp, even, unsignedRoundingMode);
123+
const rounded = remainder.isZero() ? r1 : ApplyUnsignedRoundingMode(r1, r2, cmp, even, unsignedRoundingMode);
124124
const result = sign === 'positive' ? rounded : rounded.multiply(-1);
125125
return TimeDuration.#validateNew(result, 'rounding');
126126
}

0 commit comments

Comments
 (0)