From 5f5b216428c54336ac87ca1b759f6e32678a62ae Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Tue, 26 Sep 2023 19:15:44 +0530 Subject: [PATCH] fix: Performance issue in Gantt diagram --- .../src/diagrams/gantt/ganttRenderer.js | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js index 935ecc9285..554df8c3c1 100644 --- a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js +++ b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js @@ -497,20 +497,37 @@ export const draw = function (text, id, version, diagObj) { * @param w * @param h * @param tasks - * @param excludes - * @param includes + * @param {unknown[]} excludes + * @param {unknown[]} includes */ function drawExcludeDays(theGap, theTopPad, theSidePad, w, h, tasks, excludes, includes) { - const minTime = tasks.reduce( - (min, { startTime }) => (min ? Math.min(min, startTime) : startTime), - 0 - ); - const maxTime = tasks.reduce((max, { endTime }) => (max ? Math.max(max, endTime) : endTime), 0); - const dateFormat = diagObj.db.getDateFormat(); + if (excludes.length === 0 && includes.length === 0) { + return; + } + + let minTime; + let maxTime; + for (const { startTime, endTime } of tasks) { + if (minTime === undefined || startTime < minTime) { + minTime = startTime; + } + if (maxTime === undefined || endTime > maxTime) { + maxTime = endTime; + } + } + if (!minTime || !maxTime) { return; } + if (dayjs(maxTime).diff(dayjs(minTime), 'year') > 10) { + console.warn( + 'The difference between the min and max time is more than 10 years. This will cause performance issues. Skipping drawing exclude days.' + ); + return; + } + + const dateFormat = diagObj.db.getDateFormat(); const excludeRanges = []; let range = null; let d = dayjs(minTime);