티스토리 뷰

http://blog.stevenlevithan.com/archives/faster-than-innerhtml

 

  1. /* This is much faster than using (el.innerHTML = value) when there are many
    existing descendants, because in some browsers, innerHTML spends much longer
    removing existing elements than it does creating new ones. */

    function replaceHtml(el, html) {
    var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
    /*@cc_on // Pure innerHTML is slightly faster in IE
    oldEl.innerHTML = html;
    return oldEl;
    @*/
    var newEl = oldEl.cloneNode(false);
    newEl.innerHTML = html;
    oldEl.parentNode.replaceChild(newEl, oldEl);
    /* Since we just removed the old element from the DOM, return a reference
    to the new element, which can be used to restore variable references. */

    return newEl;
    };

innerHTML이 createElement/appendChild조합보다 빠르다는 것은 많이 알려진 사실인데... 그것보다 더 빠른 방법이 있다고 하는데...
원 출처에서는 장황하게 설명하고 있지만, 핵심은 cloneNode를 통해서 기존의 노드를 document로부터 detach한 상태에서 innerHTML에 접근한 덕분에 성능이 향상되는 것으로 보인다.

이 글은 스프링노트에서 작성되었습니다.

'hacking > web' 카테고리의 다른 글

dojo 1.0을 위한 grid 위젯은 TurboGrid  (0) 2007.09.17
SVG for All  (2) 2007.09.14
bloglines 3.0beta  (0) 2007.09.07
Web 2.0 ... The Machine is Us/ing Us  (0) 2007.02.26
FireBug 1.0  (0) 2007.01.28
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함