블록킹 없이 스크립트 로딩하긔
원문은 여기서 보세요.
Loading Scripts Without Blocking
블록킹 없이 스크립트 로딩하긔
April 27, 2009 10:49 PM
This post is based on Chapter 4 from Even Faster Web Sites, the follow-up to High Performance Web Sites. Posts in this series include: chapters and contributing authors, Splitting the Initial Payload, Loading Scripts Without Blocking, Coupling Asynchronous Scripts, and Positioning Inline Scripts.
As more and more sites evolve into “Web 2.0″ apps, the amount of JavaScript increases.
사이트가 Web 2.0 어플 형태로 개발 될수록, JavaScript 의 양은 늘어납니다.
This is a performance concern because scripts have a negative impact on page performance.
스크립트는 페이지 성능에 안 좋은 영향을 끼치기 때문에 이는 성능 문제를 야기 시킬 수 있습니다.
Mainstream browsers (i.e., IE 6 and 7) block in two ways:
주류 브라우저(IE6, 7 같은)에서 블록 될 수 있는 경우에는 두 가지가 있습니다.
- Resources in the page are blocked from downloading if they are below the script.
- 스크립트 내에 있는 페이지의 리소스가 다운로드 때문에 블록 된 경우
- Elements are blocked from rendering if they are below the script.
- 스크립트 내에 있는 엘리먼트의 렌더링 때문에 블록 된 경우
The Scripts Block Downloads example demonstrates this.
여기 Scripts Block Downloads 예제 가 있습니다.
It contains two external scripts followed by an image, a stylesheet, and an iframe.
이 예제는 두 개의 외부 스크립트에 이어서 이미지, CSS, IFRAME 을 포함하고 있는데요,
The HTTP waterfall chart from loading this example in IE7 shows that the first script blocks all downloads, then the second script blocks all downloads, and finally the image, stylesheet, and iframe all download in parallel.
이 예제를 IE7 에서 돌렸을 때의 HTTP 전송상황을 보면 맨 처음 스크립트의 다운로드가 끝날 때까지 블록 됨을 확인 할 수 있습니다. 그리고 나서 두번째 스크립트도 다운로드가 끝날 때까지 블록됩니다. 마지막으로 이미지, CSS, IFRAME 은 다운로드가 병렬적으로 진행됩니다.
Watching the page render, you’ll notice that the paragraph of text above the script renders immediately.
페이지 렌더링 되는 걸 보면 스크립트 위에 있는 텍스트 문장은 즉시 렌더링 되는 걸을 확인 할 수 있습니다.
However, the rest of the text in the HTML document is blocked from rendering until all the scripts are done loading.
하지만 HTML 문서의 나머지 텍스트는 스크립트 로딩이 모두 끝날 때까지 렌더링이 되지 않고 블록됩니다.

Scripts block downloads in IE6&7, Firefox 2&3.0, Safari 3, Chrome 1, and Opera
IE6~7 와 Firefox 2~3, Safari 3, Chrome 1, Opera 에서의 스크립트 다운로드
Browsers are single threaded, so it’s understandable that while a script is executing the browser is unable to start other downloads.
스크립트가 실행되는 동안 브라우저가 다른 다운로드를 시작하는 것이 불가능하다는 것은 브라우저가 싱글 쓰레드로 동작하기 때문에 이해가 되는 부분입니다.
But there’s no reason that while the script is downloading the browser can’t start downloading other resources.
하지만 스크립트가 다운로드 되는 동안 브라우저가 다른 리소스의 다운로드를 시작하는게 불가능 하다는 건 이해가 되질 않습니다.
And that’s exactly what newer browsers, including Internet Explorer 8, Safari 4, and Chrome 2, have done.
최근의 브라우저인 IE8, Safari 4, Chrome 2 에서는 이러한 문제가 수정되었습니다.
The HTTP waterfall chart for the Scripts Block Downloads example in IE8 shows the scripts do indeed download in parallel, and the stylesheet is included in that parallel download.
앞서 봤던 에제를 IE8 에서 돌려보고 HTTP 전송상황을 보면 스크립트의 다운로드가 병렬적으로 진행되고, CSS 도 동시에 다운로드가 진행되는 것을 볼 수 있습니다.
But the image and iframe are still blocked. Safari 4 and Chrome 2 behave in a similar way.
하지만 이미지나 IFRAME 은 여전히 블록됩니다. Safari 4 와 Chrome 2 도 마찬가지입니다.
Parallel downloading improves, but is still not as much as it could be.
병렬 다운로드 기능이 향상되긴 했지만 아직 좀 부족합니다.

Scripts still block, even in IE8, Safari 4, and Chrome 2
IE8, Safari 4, Chrome 2 에서도 스크립트가 블록됨
Fortunately, there are ways to get scripts to download without blocking any other resources in the page, even in older browsers. Unfortunately, it’s up to the web developer to do the heavy lifting.
다행히도 페이지 내에 있는 어떠한 리소스라도 블록 없이 스크립트를 로딩할 수 있는 몇 가지 방법이 있습니다.
There are six main techniques for downloading scripts without blocking:
블록 없이 스크립트를 다운로드 받은 수 있는 방법은 크게 6가지 입니다.
- XHR Eval - Download the script via XHR and eval() the responseText.
- XHR Eval - 스크립트를 XHR 로 다운 받아서 responseText 을 eval() 로 실행 시키는 방법
- XHR Injection - Download the script via XHR and inject it into the page by creating a script element and setting its text property to the responseText.
- XHR Injection - 스크립트를 XHR 로 다운 받아서 responseText 을 새로 만든 <SCRIPT> 엘리먼트 내의 text 프로퍼티로 집어 넣는 방법
- Script in Iframe - Wrap your script in an HTML page and download it as an iframe.
- Script in Iframe - 스크립트를 IFRAME 페이지 내에서 다운받는 방법
- Script DOM Element - Create a script element and set its src property to the script’s URL.
- Script DOM Element - <SCRIPT> 엘리먼트를 만들고 그거의 src 을 스크립트의 URL 로 지정하는 방법
- Script Defer - Add the script tag’s defer attribute. This used to only work in IE, but is now in Firefox 3.1.
- Script Defer - <SCRIPT> 태그에 defer 어트리뷰트를 지정하는 방법. 이 방법은 현재 IE 랑 FireFox 3.1 에서만 사용 가능하다.
- document.write Script Tag - Write the <script src=""> HTML into the page using document.write. This only loads script without blocking in IE.
- document.write Script Tag - <SCRIPT src=””> HTML 코드를 document.write 를 사용해 찍어주는 방법. 오직 IE 에서만 이 방법으로 블록 되는 걸 피할 수 있다.
You can see an example of each technique using Cuzillion.
Cuzillion 를 사용해서 각각의 방법에 대한 예제를 확인해 보면,
It turns out that these techniques have several important differences, as shown in the following table.
아래 테이블에서 보여주는 것 처럼, 이러한 방법들은 몇가지 중요한 차이점을 가지고 있다는 걸 알 수 있습니다.
Most of them provide parallel downloads, although Script Defer and document.write Script Tag are mixed.
Script Defer 와 document.write Script Tag 방법은 브라우저 마다 다르게 동작하기는 하지만 대부분의 방법은 사용 했을 때 병렬 다운로드가 가능하다.
Some of the techniques can’t be used on cross-site scripts, and some require slight modifications to your existing scripts to get them to work.
몇 가지 방법은 크로스-사이트 스크립트에서 사용할 수 없습니다. 그리고 몇 가지는 기존에 사용하던 스크립트를 약간 수정해야 합니다.
An area of differentiation that’s not widely discussed is whether the technique triggers the browser’s busy indicators (status bar, progress bar, tab icon, and cursor).
어떤 방법이 브라우저 로딩 중 상태(상태바나 로딩바, 탭 아이콘, 커서 같은)를 보여 주는지 여부에 대한 내용까지 넓게 논의 되지는 않았습니다.
If you’re loading multiple scripts that depend on each other, you’ll need a technique that preserves execution order.
만약 서로가 의존성을 가지고 있는 여러 개의 스크립트를 로딩해야 한다면, 스크립트의 실행 순서가 유지되는 방법이 필요합니다.
|
Technique 방법 |
Parallel Downloads 병렬 다운로드 |
Domains can Differ 도메인이 달라도 되나 |
Existing Scripts |
Busy Indicators 로딩 중 표시 여부 |
Ensures Order 순서가 보장되나 |
Size (bytes) 사이즈 |
|
XHR Eval |
IE, FF, Saf, Chr, Op |
no |
no |
Saf, Chr |
- |
~500 |
|
XHR Injection |
IE, FF, Saf, Chr, Op |
no |
yes |
Saf, Chr |
- |
~500 |
|
Script in Iframe |
IE, FF, Saf, Chr, Op |
no |
no |
IE, FF, Saf, Chr |
- |
~50 |
|
Script DOM Element |
IE, FF, Saf, Chr, Op |
yes |
yes |
FF, Saf, Chr |
FF, Op |
~200 |
|
Script Defer |
IE, Saf4, Chr2, FF3.1 |
yes |
yes |
IE, FF, Saf, Chr, Op |
IE, FF, Saf, Chr, Op |
~50 |
|
document.write Script Tag |
IE, Saf4, Chr2, Op |
yes |
yes |
IE, FF, Saf, Chr, Op |
IE, FF, Saf, Chr, Op |
~100 |
The question is: Which is the best technique?
그렇다면 어떤 것이 최선의 방법일까요?
The optimal technique depends on your situation.
바로 당신의 상황에 맞는 게 최선의 방법입니다.
This decision tree should be used as a guide.
여기 결정 Tree 가 도움이 될 수 있을 것 같습니다.
It’s not as complex as it looks.
생각보다 복잡하지 않습니다.
Only three variables determine the outcome: is the script on the same domain as the main page, is it necessary to preserve execution order, and should the busy indicators be triggered.
단지 세 개의 값들로 결정 짓습니다 : 스크립트가 메인 페이지에 대해서 같은 도메인에 있는지, 실행 순서가 지켜져야 되는지, 로딩 중 표시가 보여야 하는지

Ideally, the logic in this decision tree would be encapsulated in popular HTML templating languages (PHP, Python, Perl, etc.) so that the web developer could just call a function and be assured that their script gets loaded using the optimal technique.
이상적으로는 웹개발자가 가장 최적의 방법을 사용해서 스크립트를 읽어 들이는 것을 보장하고 함수를 호출하기 위해, 이러한 결정 Tree 에서의 논리는 가장 인기있는 HTML 템플릿 언어(PHP, Python, Perl 등)로 캡슐화 되어야 합니다.
In many situations, the Script DOM Element is a good choice.
많은 경우, Script DOM Element 를 사용하는 방법은 좋은 선택 입니다..
It works in all browsers, doesn’t have any cross-site scripting restrictions, is fairly simple to implement, and is well understood.
해당 방법은 크로스-사이트 스크립트 문제가 없이 모든 브라우저에서 잘 동작하고 구현하기 꽤 간단하며, 이해하기도 괜찮습니다..
The one catch is that it doesn’t preserve execution order across all browsers.
한 가지 아쉬운 점은 실행 순서를 유지 하는 것은 일부 브라우저에서만 지원한다는 점 입니다.
If you have multiple scripts that depend on each other, you’ll need to concatenate them or use a different technique.
만약 서로간 의존성을 가진 여러 개의 스크립트 파일을 사용하고 있다면, 그 파일들을 하나의 파일로 합쳐서 사용하거나, 아니면 다른 방법을 써야 하고,
If you have an inline script that depends on the external script, you’ll need to synchronize them.
외부 스크립트에 의존적인 inline 인라인 스크립트가 있다면, 스크립트 수행이 동시에 일어나야 합니다.
I call this “coupling” and present several ways to do this in Coupling Asynchronous Scripts.
저는 이걸 “coupling” 이라고 부르고요, Coupling Asynchronous Scripts 를 통해서 몇 가지 방법을 알려주고 있습니다.
- 영양가 있는 포스팅인가요
- (총 4분이 투표해서 4.0점) 4.0점
Trackback Address :: http://hooriza.com/trackback/1853
-
Tracked from 옷장수네 집
2009/05/17 16:52 삭제
Subject: 자바스크립트를 동적으로 로딩하기
자바스크립트를 사용하여 프로그램을 작성하다 보면, 로딩에 사용하지 않는 코드라던가, 특정한 경우에만 사용하게 되는 코드들을 선택적으로 로딩하고 싶은 욕구가 발생하게 됩니다. 대충 이런? ^^ Dynamic Script Loading 동적으로 자바스크립트를 로딩하는 방법 중 하나는 script 태그를 자바스크립트 코드에서 직접 생성하는 것인데요. 다음과 같이 script 태그를 생성하고 src에 로딩할 주소를 넣음으로서 로딩하게 됩니다. var..



