Repository: typeorm/typeorm
File: src/persistence/SubjectDatabaseEntityLoader.ts (TypeScript)

Code before:
                    this.queryRunner.connection.driver.options.type ===
                    "mongodb"
                ) {
                    const mongoRepo =
                        this.queryRunner.manager.getRepository<ObjectLiteral>(
                            subjectGroup.target,
                        ) as MongoRepository<ObjectLiteral>
                    entities = await mongoRepo.findByIds(allIds, findOptions)
                } else {
                    entities = await this.queryRunner.manager
                        .getRepository<ObjectLiteral>(subjectGroup.target)
                        .createQueryBuilder()
                        .setFindOptions(findOptions)
                        .whereInIds(allIds)
                        .getMany()
                }

                // now when we have entities we need to find subject of each entity
                // and insert that entity into database entity of the found subjects
                entities.forEach((entity) => {
                    const subjects = this.findByPersistEntityLike(
                        subjectGroup.target,
                        entity,
                    )
                    subjects.forEach((subject) => {
                        subject.databaseEntity = entity
                        if (!subject.identifier)
                            subject.identifier =
                                subject.metadata.hasAllPrimaryKeys(entity)
                                    ? subject.metadata.getEntityIdMap(entity)
                                    : undefined
                    })
                })

                // this way we tell what subjects we tried to load database entities of
                for (const subject of allSubjects) {
                    subject.databaseEntityLoaded = true
                }
            },
        )

        await Promise.all(promises)
    }

    // ---------------------------------------------------------------------
    // Protected Methods
    // ---------------------------------------------------------------------

    /**
     * Finds subjects where entity like given subject's entity.
     * Comparison made by entity id.

Review comment (performance):
`forEach` offers here better performance than `filter` which creates new array
Another thing, we used to iterate over entire `subjects` array which might be huge, now we operate only on relevant subjects perhaps counterintuitively called `allSubjects`

Code after:
                if (
                    this.queryRunner.connection.driver.options.type ===
                    "mongodb"
                ) {
                    const mongoRepo =
                        this.queryRunner.manager.getRepository<ObjectLiteral>(
                            subjectGroup.target,
                        ) as MongoRepository<ObjectLiteral>
                    entities = await mongoRepo.findByIds(allIds, findOptions)
                } else {
                    entities = await this.queryRunner.manager
                        .getRepository<ObjectLiteral>(subjectGroup.target)
                        .createQueryBuilder()
                        .setFindOptions(findOptions)
                        .whereInIds(allIds)
                        .getMany()
                }

                // Now when we have entities we need to find subject of each entity
                // and insert that entity into database entity of the found subjects.
                // A single entity can be applied to many subjects as there might be duplicates.
                // This will likely result in the same row being updated multiple times during a transaction.
                entities.forEach((entity) => {
                    const entityId =
                        allSubjects[0].metadata.getEntityIdMap(entity)
                    allSubjects.forEach((subject) => {
                        if (subject.databaseEntity) return
                        if (OrmUtils.compareIds(subject.identifier, entityId))
                            subject.databaseEntity = entity
                    })
                })

                // this way we tell what subjects we tried to load database entities of
                for (const subject of allSubjects) {
                    subject.databaseEntityLoaded = true
                }
            },
        )

        await Promise.all(promises)
    }

    // ---------------------------------------------------------------------
    // Protected Methods
    // ---------------------------------------------------------------------

    /**
     * Groups given Subject objects into groups separated by entity targets.
     */
    protected groupByEntityTargets(): {
        target: Function | string