作者:empty 页数:349 出版社:empty |
JavaScript performance in the browser is arguably the most important usa bit y issue facing developers.Theproblem is complex because of the blocking nature of JavaScript, which is to say that nothing el sc can happenwhile Java Seri pt code is being executed.In fae, mos throws ers use a single process for both userinterface(UI)updates and JavaScript execution, so only one can happen at any given moment in time.The longer Java Seri pttakes to execute, the longer it takes before the browser is free to respond to userinputJavaScript在浏览器中的性能。可认为是开发者所要面对的最重要的可用性问题。此问墨因JavaScript的阻塞特征而复杂, 也就是说, Java Seri pt运行时其他的事情不脆被调苋器处理。事实上, 大多数苋器使用节进料处理U史新和Java Seri pt运行等多个任务, 同一时间只能有一个任务被执行。JavaScript运行了多长时间,那么在浏览器空闲下来响应用户输入之前的等待时间就有多长。On a basic level, this means that the very presence of as sc npt tag is enough to make the page wait for theseri pt to be parsed and executed.Whether the actual Java Seri pt code is inline with the tag or inch uded in anexternal file is irrelevant; the page download and rendering must stop and wat for these rip t to complete beforeproceeding.This is a necessary part of the page's lifecycle because the script may cause changes to the pagewhile execu ing.The typical example is using document write() in the middle of a page(as often used byadvertisements) .For example:从基本层面说, 这意味看 When the browser encounters a~seri pp tag, as in this HTML page, there is no way of knowing whether theJava Seri pt wil insert content into the cp, introduce add tonal elements, or pet haps even close the tag.Thereforethe browser stops processing the page as it comes in, executes the JavaScript code, then continues parsing andrendering the page.The same takes place for Java Seri pt loaded using the s re attr bute, the browser must firstdownload the code from the exte mal file, which takes time, and then parse and execute the code.Page renderingand user inter a eton are completely blocked during this time当器选 Though this code seems innocuous.it actually has a severe performance issue:there are three Java Seri pt filesbeing loaded in the-head.Since cach seri pt tag blocks the page from continuing to render until it has fullydownloaded and executed the JavaScript code, the perceived performance of this page will suffer.Keep in mindthat browsers don't start rendering anything on the page until the opening body tag is encountered.Puttingscripts at the top of the page in this way typically leads to a noticeable delay, often in the form of a blank whitepage, he for e the user can even begin reading or otherwise interacting with the page.To get a good understandingof how this occurs, its useful to look at a waterfall diagram showing when each resource is downloaded.FigureI-1showswheneachseript and the stylesheet file get downloaded as the page as loading虽然这些代叫看起来是无害的。但它们确实存在性能问题:在 Figure 1-1 shows an interesting patte mn.The first JavaScript file begins to download and blocks any of theother files from downloading in the meantime.Further, there is a delay between the time at which file ljs iscompletely downloaded and the time at which file 2js begins to download.That space is the time it takes for thecode contained in file l.js to fully execute.Each file must wait until the previous one has been downloaded andexecuted before the next download can begin.In the meantime, the user is met with a blank screen as the files arebeing downloaded one at at ie.This is the behavior of most major browsers today1-1是一个令人感兴趣的板板。第一个JavaScript文件开始下载, 开用塞了其他文件的下载过程、进一, fl el.js下完之后利fle2js开始下载之前有一个延时, 这是fl el.js完全运行所需的时间。每个文件必须等待前一个文件下载完成开运行完之后,才能开始自己的下载过程。当这些文件下载时,用户面对一个空白的屏幕。这就是今天大多数润览器的行为模式。InternetExplorer 8.Firefox 3.5, Safari 4, and Chrome 2 all allow parallel downloads of JavaScript files.This isgood news because the