코딩/Javascript

[TIL - 23.12.07] 메모리 관리 관점에서 바라본 &&와 ||

AMD만세 2023. 12. 7. 15:14

1. Vue2

1) 오늘 배운 내용

예를 들어, 환경설정 옵션값이 true/false 에 따라,

특정 input값이 비어있다면 '설정해주세요'라는 메시지 팝업을 띄운다고 하자.

if ((this.place === null || this.place === '') && this.isRequiredInputPlace) {
  this.$commonFunction.showDialog_alert_message('장소를 입력해주세요.');
}

컴파일러가 코드를 읽을 때 

true || 면 그냥 true라고 인식해버림.

false && 면 그냥 false라고 인식해버림.

조건이 여러개일 때, 기준을 옵션값에 맞춰주면

if (this.isRequiredInputPlace && (this.place === null || this.place === '')) {
  this.$commonFunction.showDialog_alert_message('장소를 입력해주세요.');
}

동작하는 건 똑같지만, 메모리 관리 관점에서 좀 더 효율적이다.