diff --git a/lib/timeWindowQuantiles.js b/lib/timeWindowQuantiles.js index 9e7228dd..a67a9381 100644 --- a/lib/timeWindowQuantiles.js +++ b/lib/timeWindowQuantiles.js @@ -43,18 +43,17 @@ class TimeWindowQuantiles { } function rotate() { - let timeSinceLastRotateMillis = Date.now() - this.lastRotateTimestampMillis; - while ( - timeSinceLastRotateMillis > this.durationBetweenRotatesMillis && - this.shouldRotate - ) { - this.ringBuffer[this.currentBuffer] = new TDigest(); - - if (++this.currentBuffer >= this.ringBuffer.length) { - this.currentBuffer = 0; + if (this.shouldRotate) { + let timeSinceLastRotateMillis = Date.now() - this.lastRotateTimestampMillis; + while (timeSinceLastRotateMillis > this.durationBetweenRotatesMillis) { + this.ringBuffer[this.currentBuffer] = new TDigest(); + + if (++this.currentBuffer >= this.ringBuffer.length) { + this.currentBuffer = 0; + } + timeSinceLastRotateMillis -= this.durationBetweenRotatesMillis; + this.lastRotateTimestampMillis += this.durationBetweenRotatesMillis; } - timeSinceLastRotateMillis -= this.durationBetweenRotatesMillis; - this.lastRotateTimestampMillis += this.durationBetweenRotatesMillis; } return this.ringBuffer[this.currentBuffer]; }