크롬 개발자도구 → [performance] → 좌측 상단 ↩️ 새로고침 버튼(Start profiling) 클릭
removeSpecialCharacter
로 인한 병목현상 발생효율적인 함수로 개선
작업하는 양을 줄이기
// 기존 코드
function removeSpecialCharacter(str) {
const removeCharacters = ["#","_","*","~","&",";","!","[","]","`",">","\\n","=","-",];
let _str = str;
let i = 0, j = 0;
for (i = 0; i < removeCharacters.length; i++) {
j = 0;
while (j < _str.length) {
if (_str[j] === removeCharacters[i]) {
_str = _str.substring(0, j).concat(_str.substring(j + 1));
continue;
}
j++;
}
}
return _str;
}
// 개선된 코드
function removeSpecialCharacter(str) {
let _str = str.substring(0, 300);
_str.replace(/[\\#\\_\\*\\~\\&\\;\\~\\[\\]\\`\\n\\=\\-\\>\\~]/g, "");
return _str;
}