중요한 DOM에서 작업하는 동안 성능 측면 에서 최상의 답변을 찾고있었습니다 .
눈꺼풀 없음의 대답은 자바 스크립트를 사용하면 성능이 가장 좋을 것이라고 지적했습니다.
제거 할 섹션 내부에 복잡한 DOM 구성을 사용하여 5,000 줄과 400,000 자에 대해 다음 실행 시간 테스트를 수행했습니다. 자바 스크립트를 사용할 때 편리한 이유로 클래스 대신 ID를 사용하고 있습니다.
$ .unwrap () 사용
$('#remove-just-this').contents().unwrap();
201.237ms
$ .replaceWith () 사용
var cnt = $("#remove-just-this").contents();
$("#remove-just-this").replaceWith(cnt);
156.983ms
자바 스크립트에서 DocumentFragment 사용
var element = document.getElementById('remove-just-this');
var fragment = document.createDocumentFragment();
while(element.firstChild) {
fragment.appendChild(element.firstChild);
}
element.parentNode.replaceChild(fragment, element);
147.211ms
결론
성능면에서 상대적으로 큰 DOM 구조에서도 jQuery와 자바 스크립트 사용의 차이는 크지 않습니다. 놀랍게도 $.unwrap()에 비해 가장 비용이 많이 드는입니다 $.replaceWith(). 테스트는 jQuery 1.12.4로 수행되었습니다.