diff --git a/debug_toolbar/static/debug_toolbar/js/timer.js b/debug_toolbar/static/debug_toolbar/js/timer.js index bb777ee10..a88ab0d15 100644 --- a/debug_toolbar/static/debug_toolbar/js/timer.js +++ b/debug_toolbar/static/debug_toolbar/js/timer.js @@ -5,15 +5,29 @@ function insertBrowserTiming() { timingEnd = performance.timing.loadEventEnd, totalTime = timingEnd - timingOffset; function getLeft(stat) { - return ((performance.timing[stat] - timingOffset) / totalTime) * 100.0; + if (totalTime !== 0) { + return ( + ((performance.timing[stat] - timingOffset) / totalTime) * 100.0 + ); + } else { + return 0; + } } function getCSSWidth(stat, endStat) { - let width = - ((performance.timing[endStat] - performance.timing[stat]) / - totalTime) * - 100.0; - // Calculate relative percent (same as sql panel logic) - width = (100.0 * width) / (100.0 - getLeft(stat)); + let width = 0; + if (totalTime !== 0) { + width = + ((performance.timing[endStat] - performance.timing[stat]) / + totalTime) * + 100.0; + } + const denominator = 100.0 - getLeft(stat); + if (denominator !== 0) { + // Calculate relative percent (same as sql panel logic) + width = (100.0 * width) / denominator; + } else { + width = 0; + } return width < 1 ? "2px" : width + "%"; } function addRow(tbody, stat, endStat) { diff --git a/docs/changes.rst b/docs/changes.rst index 5ca3a17a9..1cf12d344 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -4,6 +4,7 @@ Change log Pending ------- +* Added protection against division by 0 in timer.js * Auto-update History panel for JavaScript ``fetch`` requests. * Support `HTMX boosting `__ and re-rendering the toolbar after the DOM has been replaced. This reworks @@ -11,7 +12,6 @@ Pending This means we'll have slightly slower performance, but it's easier to handle re-rendering the toolbar when the DOM has been replaced. - 3.7.0 (2022-09-25) ------------------