sitelink1  
sitelink2  
sitelink3 http://1 
sitelink4 http://ko 
extra_vars5 http://firejune.com/1174 
extra_vars6 sitelink1 
Steven Levithan씨는 자신의 블로그에 흥미로운 내용의 글을 작성했다. "이 포스트는 innerHTML vs. W3C DOM 메서드들의 찬반양론에 관한 것이 아니다."로 시작하는 범상치 않은 이 포스트innerHTML의 성능을 끌어올리는 방법을 소개한다. 몇몇 브라우저(주목할 만한 녀석은 파이어폭스)에서는 일반적으로 DOM 메서드를 사용하는 것 보다는 innerHTML이 훨씬 빠르다는 사실을 우리는 잘 알고 있다. RegexPal 을 개발하면서 주된 성능 최적화 작업을 통해 이 사실을 알아냈다. RegexPal은 자바스크립트를 이용하여 문자열을 정규식으로 비교하고 동일한 문자열을 강조하는 테스트 사이트이다.

RegexPal에서는 모든 키다운 이벤트에 문자열 강조를 위한 수전개의 엘리먼트 파괴와 창조 트리거들을 잠제적으로 가지고 있다. 이런 경우 el.innerHTML += str 또는 el.innerHTML = ""만을 가지고는 수천개의 자식 엘리먼트에 접근하여 업데이트하는 과정의 성능을 끌어올리지 못한다. 그래서 아래와같은 replaceHtml 함수를 개발했고 innerHTML과 비교한 성능 측정 페이지를 만들었다. el.innerHTML = newHtml 은 el = replaceHtml(el, newHtml) 처럼 사용할 수 있다.

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;
};

파이어폭스 2.0.0.6에서 태스트한 결과 다음처럼 성능이 향상된 결과를 얻을 수 있었다.
1000 elements...
innerHTML (destroy only): 156ms
innerHTML (create only): 15ms
innerHTML (destroy & create): 172ms
replaceHtml (destroy only): 0ms (faster)
replaceHtml (create only): 15ms (~ same speed)
replaceHtml (destroy & create): 15ms (11.5x faster)

15000 elements...
innerHTML (destroy only): 14703ms
innerHTML (create only): 250ms
innerHTML (destroy & create): 14922ms
replaceHtml (destroy only): 31ms (474.3x faster)
replaceHtml (create only): 250ms (~ same speed)
replaceHtml (destroy & create): 297ms (50.2x faster)

엘리먼트의 수가 많을 수록 속도가 더욱 개선 되는 것을 알 수 있다. 사파리 브라우저의 성능향상률이 가장 좋았으며, 오페라 브라우저는 근소한 차이로 innerHTML 보다는 replaceHtml이 더 빠른 결과를 보여주었다. 특이하게도 오페라 9.23과 IE계열 브라우저에서는 다른 브라우저들과 다르게 더 느린 현상이 나타났다. 그럼에도 불구하고 이 함수를 사용하는 이유는 IE는 전형적으로 innerHTML의 처리가 타 브라우저에 비해 월등히 빠르기 때문에 1.1x 느린 정도의 페널티로 여러 브라우저에서 성능향상을 기대 할 수 있기 때문이다.

주. Prototype에 대응하여 아래처럼 Element 클래스에 등록해 사용하면 $(el).replaceHTML(newHTML)과 같은 형식으로 사용할 수 있다.
Element.addMethods({
  replaceHTML: function(element, html) {
  	var oldEl = element;
  	/*@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;
  }
});
번호 제목 글쓴이 날짜 조회 수
28 InternetExplorer 의 알림 표시줄에 대한 정보 및 우회 방법 file 황제낙엽 2011.05.11 225
27 Cross Browser DHTML Modal Dialogs For Web Apps 황제낙엽 2011.05.11 171
26 Windows XP 서비스 팩 2를 위한 웹 사이트 미세 조정 황제낙엽 2011.05.11 199
25 What’s wrong with extending the DOM 황제낙엽 2011.03.28 2254
24 [MSDN] Document Object Model Prototypes (IE8) 황제낙엽 2011.03.24 847
23 Flash와 Wmode [Object element] 황제낙엽 2011.03.10 85
22 OBJECT and EMBED syntax 황제낙엽 2011.03.02 194
21 Flash OBJECT and EMBED tag attributes 황제낙엽 2011.03.02 139
20 Benchmark - W3C DOM vs. innerHTML 황제낙엽 2011.02.23 85
19 DOM Node handling 실행속도 (DOM Core vs innerHTML) 황제낙엽 2011.02.23 81
» innerHTML의 속도가 만족스럽지 않을 때 황제낙엽 2011.02.23 94
17 Embed의 주요 속성 황제낙엽 2011.02.14 171
16 Object의 주요 속성 황제낙엽 2011.02.14 337
15 웹표준으로 플래시(Flash) 출력 하기 황제낙엽 2011.02.14 100
14 Embed 를 제거하여 XHTML 표준 따라가기 황제낙엽 2011.02.14 106
13 웹표준을 지키기 위한 노력 Firefox의 HTML Validator file 황제낙엽 2011.02.10 116
12 DOCTYPE과 DTD 그리고 Quirks Mode 황제낙엽 2011.02.10 251
11 웹 표준 개발자의 IE8 고려사항(quirk) file 황제낙엽 2010.10.25 131
10 W3C CSS validator installation file 황제낙엽 2010.08.17 160
9 사이즈가 조절되는 Background 이미지 - 개선판 file 황제낙엽 2009.06.28 86