diff --git a/packages/nx/src/tasks-runner/tasks-schedule.ts b/packages/nx/src/tasks-runner/tasks-schedule.ts index 97ce3f6304f52..3374c536314a1 100644 --- a/packages/nx/src/tasks-runner/tasks-schedule.ts +++ b/packages/nx/src/tasks-runner/tasks-schedule.ts @@ -28,6 +28,7 @@ export class TasksSchedule { private completedTasks = new Set(); private scheduleRequestsExecutionChain = Promise.resolve(); private estimatedTaskTimings: Record = {}; + private projectDependencies: Record = {}; constructor( private readonly projectGraph: ProjectGraph, @@ -42,6 +43,15 @@ export class TasksSchedule { Object.values(this.taskGraph.tasks).map((t) => t.target) ); } + + for (const project of Object.values(this.taskGraph.tasks).map( + (t) => t.target.project + )) { + this.projectDependencies[project] ??= findAllProjectNodeDependencies( + project, + this.reverseProjectGraph + ).length; + } } public async scheduleNextTasks() { @@ -125,14 +135,8 @@ export class TasksSchedule { const project1 = this.taskGraph.tasks[taskId1].target.project; const project2 = this.taskGraph.tasks[taskId2].target.project; - const project1NodeDependencies = findAllProjectNodeDependencies( - project1, - this.reverseProjectGraph - ).length; - const project2NodeDependencies = findAllProjectNodeDependencies( - project2, - this.reverseProjectGraph - ).length; + const project1NodeDependencies = this.projectDependencies[project1]; + const project2NodeDependencies = this.projectDependencies[project2]; const dependenciesDiff = project2NodeDependencies - project1NodeDependencies;