자바스크립트 이중for문 최적화

송용준·2025년 2월 14일
for (var iLoop20 = 0; iLoop20 < this.ds_Detail20.rowcount; iLoop20++) {
    for (var iLoop10 = 0; iLoop10 < this.ds_Detail10.rowcount; iLoop10++) {
        if (this.ds_Detail20.getColumn(iLoop20, "SYST_ID") === this.ds_Detail10.getColumn(iLoop10, "SYST_ID")) {
            this.ds_Detail20.setColumn(iLoop20, "s_conf", "1");
            break;  // 이미 찾았으면 나머지 비교는 불필요하니 break로 성능 향상
        }
    }
}
// Set에 detail10의 SYST_ID를 미리 저장
var detail10SystIdSet = new Set();
for (var iLoop10 = 0; iLoop10 < this.ds_Detail10.rowcount; iLoop10++) {
    detail10SystIdSet.add(this.ds_Detail10.getColumn(iLoop10, "SYST_ID"));
}

// detail20의 SYST_ID가 Set에 있는지 확인
for (var iLoop20 = 0; iLoop20 < this.ds_Detail20.rowcount; iLoop20++) {
    if (detail10SystIdSet.has(this.ds_Detail20.getColumn(iLoop20, "SYST_ID"))) {
        this.ds_Detail20.setColumn(iLoop20, "s_conf", "1");
    }
}

위에 이중for문을 set으로 바꿈
Set은 해시 알고리즘을 사용해 데이터를 저장
각 값의 해시 코드를 계산해 특정 메모리 위치에 바로 접근하기 때문에, 검색 시간은 항상 일정

이중 for문 시간복잡도 : O(m × n)
Set 사용 시간복잡도 : O(m + n)

profile
용용

0개의 댓글