Batch update slow

I am trying to save a tasks critical path status to the database. I am using the following code, but the updateTask() call causes it to run extremely slowly. What could be causing this?

            gantt.eachTask((task) => {
              const isCritical = gantt.isCriticalTask(task);
              if (
                task.critical_path !== isCritical &&
                task.type !== gantt.config.types.placeholder
              ) {
                task.critical_path = isCritical;
                // console.log('%s is %s', task.text, task.critical_path);
                gantt.updateTask(task.id);
              }
            });
          }, true);```

Hello,
If you have a lot of tasks and links, it will take some time to calculate the critical path.
Also, when you calculate the critical path for a single task, Gantt does that for all its linked tasks. And when you tell Gantt to calculate the critical path for its linked tasks, Gantt doesn’t actually recalculate the critical path. Instead, it uses the value from the cache.
But if you update a task, the cache is reset, and Gantt needs to calculate the critical path again.
With your code, it is calculated the same number of times the updateTask is called.

If you update tasks after the critical path is calculated, it will take less time.

Here is an example of how to make it better:

    gantt.batchUpdate(function () {
        const updates = [];
        gantt.eachTask((task) => {
            const isCritical = gantt.isCriticalTask(task);
            if (
                task.critical_path !== isCritical &&
                task.type !== gantt.config.types.placeholder
            ) {
                task.critical_path = isCritical;
                // console.log('%s is %s', task.text, task.critical_path);
                updates.push(task.id);
                // gantt.updateTask(task.id);
            }
        })
        updates.forEach((id) => {
            gantt.updateTask(id);
        })
    });

Here is the snippet:
https://snippet.dhtmlx.com/omy6xmud

1 Like