SlideShare ist ein Scribd-Unternehmen logo
1 von 155
Downloaden Sie, um offline zu lesen
High Performance JavaScript
  Nicholas C. Zakas | Amazon DevCon | April 27, 2011
Who's this guy?




         Presentation       Contributor,
           Architect     Creator of YUI Test




Author     Lead Author   Contributor           Lead Author
@slicknet



(Complaints: @robertduffy)
Does JavaScript performance
         matter?
After all, all browsers now have
     optimizing JavaScript engines




Tracemonkey/    V8     Squirrelfish   Chakra   Karakan
JaegarMonkey   (all)      (4+)         (9+)    (10.5+)
    (3.5+)
So our scripts are getting really,
           really fast
Old computers ran slow applications
     Small amounts of CPU power and memory
New computers are generally faster but
       slow applications still exist
More CPU + more memory = less disciplined application development
Right???
Oh yeah, one more thing
http://jeftek.com/1942/motorola-xoom-sunspider-results/
It's still possible to write slow
JavaScript on the new, faster
       JavaScript engines
JavaScript performance
        directly
affects user experience
"Know the enemy and know yourself;
in a hundred battles you will never be in peril."

                      -Sun Tzu, The Art of War
The UI Thread
The brains of the operation
The browser UI thread is responsible for
both UI updates and JavaScript execution
           Only one can happen at a time
Jobs for UI updates and JavaScript execution
          are added to a UI queue
       Each job must wait in line for its turn to execute
<button id="btn" style="font-size: 30px; padding: 0.5em
    1em">Click Me</button>

<script type="text/javascript">
window.onload = function(){
   document.getElementById("btn").onclick = function(){
       //do something
   };
};
</script>
Before Click

UI Thread



time
                           UI Queue
When Clicked

UI Thread



time
                           UI Queue

                             UI Update

                             onclick

                             UI Update
When Clicked

UI Thread

 UI Update

time
                                   UI Queue

                                     onclick
        Draw down state
                                     UI Update
When Clicked

UI Thread

 UI Update   onclick

time
                              UI Queue

                                UI Update
When Clicked

UI Thread

 UI Update   onclick       UI Update

time
                                       UI Queue


               Draw up state
No UI updates while JavaScript is
           executing
JavaScript May Cause UI Update
<button id="btn" style="font-size: 30px; padding: 0.5em
    1em">Click Me</button>

<script type="text/javascript">
window.onload = function(){
   document.getElementById("btn").onclick = function(){
       var div = document.createElement(“div”);
       div.className = “tip”;
       div.innerHTML = “You clicked me!”;
       document.body.appendChild(div);
   };
};
</script>
A UI update must use the latest
        info available
Long-running JavaScript
          =
   Unresponsive UI
Responsive UI

UI Thread

 UI Update   JavaScript   UI Update

time
Unresponsive UI

UI Thread

 UI Update       JavaScript    UI Update

time
The longer JavaScript runs,
the worse the user experience
The runaway script timer prevents JavaScript
         from running for too long
      Each browser imposes its own limit (except Opera)
Internet Explorer
Firefox
Safari
Chrome
Runaway Script Timer Limits
• Internet Explorer: 5 million statements
• Firefox: 10 seconds
• Safari: 5 seconds
• Chrome: Unknown, hooks into normal crash
  control mechanism
• Opera: none
Does JIT compiling help?
Interpreted JavaScript

UI Thread

                Interpret

time
JITed JavaScript (1st Run)

UI Thread

Compile     Execute

time
JITed JavaScript (After 1st Run)

UI Thread

   Execute

time
How Long Is Too Long?
“0.1 second [100ms] is about the limit for
having the user feel that the system is reacting
instantaneously, meaning that no special
feedback is necessary except to display the
result.”
                                  - Jakob Nielsen
Translation:
 No single JavaScript job should
execute for more than 100ms to
     ensure a responsive UI
Recommendation:
Limit JavaScript execution to no more
              than 50ms



        measured on IE6 :)
Doing so makes your application awesome
Loadtime Techniques
Don't let JavaScript interfere with page load performance
During page load, JavaScript
takes more time on the UI thread
<!doctype html>
<html>
<head>
    <title>Example</title>
</head>
<body>
    <p>Hello world!</p>
    <script src="foo.js"></script>
    <p>See ya!</p>
</body>
</html>
Result

UI Thread

 UI Update   JavaScript   UI Update

time
Result

UI Thread

 Hello world!   foo.js   See ya!

time
Result

UI Thread

 Hello world!   Download   Parse   Run   See ya!

time




  The UI thread needs to wait for the script to
  download, parse, and run before continuing
Result

UI Thread

 Hello world!   Download    Parse   Run   See ya!



                 Variable      Constant




Download time takes the longest and is variable
Translation:
   The page doesn't render while
JavaScript is downloading, parsing, or
     executing during page load
<!doctype html>
<html>
<head>
    <title>Example</title>
</head>
<body>
    <script src="foo.js"></script>
    <p>Hello world!</p>
    <script src="bar.js"></script>
    <p>See ya!</p>
    <script src="baz.js"></script>
    <p>Uh oh!</p>
</body>
</html>
Result

UI Thread

 JavaScript   UI Update   JavaScript   UI Update   JavaScript


time




  The more scripts to download in between UI
  updates, the longer the page takes to render
Technique #1: Put scripts at the
           bottom
<!doctype html>
<html>
<head>
    <title>Example</title>
</head>
<body>
    <p>Hello world!</p>
    <p>See ya!</p>
    <script src="foo.js"></script>
</body>
</html>
Put Scripts at Bottom

UI Thread

 UI Update   UI Update   JavaScript   JavaScript   JavaScript


time




   Even if there are multiple scripts, the page
                 renders quickly
Technique #2: Combine
    JavaScript files
<!doctype html>
<html>
<head>
    <title>Example</title>
</head>
<body>
    <p>Hello world!</p>
    <p>See ya!</p>
    <script src="foo.js"></script>
    <script src="bar.js"></script>
    <script src="baz.js"></script>
</body>
</html>
UI Thread

UI Update   JavaScript   JavaScript   JavaScript


time




       Each script has overhead of downloading
UI Thread

UI Update   JavaScript


time




 Combining all of the files limits the network
overhead and gets scripts onto the page faster
<!doctype html>
<html>
<head>
    <title>Example</title>
</head>
<body>
    <p>Hello world!</p>
    <p>See ya!</p>
    <script src="foo-and-bar-and-baz.js"></script>
</body>
</html>
Technique #3: Load scripts
       dynamically
Basic Technique
var script = document.createElement("script"),
   body;
script.type = "text/javascript";
script.src = "foo.js";
body.insertBefore(script, body.firstChild);




Dynamically loaded scripts are non-blocking
Downloads no longer block the
         UI thread
<!doctype html>
<html>
<head>
    <title>Example</title>
</head>
<body>
    <p>Hello world!</p>
    <script src="foo.js"></script>
    <p>See ya!</p>
</body>
</html>
Using HTML <script>

UI Thread

 Hello world!      Download   Parse   Run   See ya!

time
<!doctype html>
<html>
<head>
    <title>Example</title>
</head>
<body>
    <p>Hello world!</p>
    <script>
    var script = document.createElement("script"),
          body = document.body;
    script.type = "text/javascript";
    script.src = "foo.js";
    body.insertBefore(script, body.firstChild);
    </script>
    <p>See ya!</p><!-- more content -->
</body>
</html>
Using Dynamic Scripts

UI Thread

 Hello world!   See ya!            Run   UI Update

time


                Download   Parse




Only code execution happens on the UI thread,
   which means less blocking of UI updates
function loadScript(url, callback){

    var script = document.createElement("script"),
        body = document.body;
    script.type = "text/javascript";

    if (script.readyState){ //IE <= 8
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
                callback();
            }
        };
    } else { //Others
        script.onload = function(){
            callback();
        };
    }

    script.src = url;
    body.insertBefore(script, body.firstChild);

}
Usage


loadScript("foo.js", function(){
      alert("Loaded!");
});
Timing Note:
 Script execution begins immediately
after download and parse – timing of
     execution is not guaranteed
Using Dynamic Scripts

UI Thread

 Hello world!                      Run   See ya!   UI Update

time


                Download   Parse




Depending on time to download and script size,
 execution may happen before next UI update
Technique #4: Defer scripts
<!doctype html>
<html>
<head>
    <title>Example</title>
</head>
<body>
    <p>Hello world!</p>
    <script defer src="foo.js"></script>
    <p>See ya!</p>
    <!-- even more markup -->
</body>
</html>
Support for <script defer>




3.5 7.0      5.0 4.0           ?
Deferred scripts begin to
  download immediately,
but don't execute until all UI
     updates complete
Using <script defer>

UI Thread

 Hello world!     See ya!     More UI   More UI   Run




time


                   Download     Parse




   Similar to dynamic script nodes, but with a
    guarantee that execution will happen last
Timing Note:
Although scripts always execute after
  UI updates complete, the order of
multiple <script defer> scripts is not
     guaranteed across browsers
Technique #5: Asynchronous
          scripts
<!doctype html>
<html>
<head>
    <title>Example</title>
</head>
<body>
    <p>Hello world!</p>
    <script async src="foo.js"></script>
    <p>See ya!</p>
    <!-- even more markup -->
</body>
</html>
Support for <script async>




3.6 7.0      5.0     ?         ?
Asynchronous scripts behave a
   lot like dynamic scripts
Using <script async>

UI Thread

 Hello world!      See ya!            Run   UI Update

time


                   Download   Parse




Download begins immediately and execution is
       slotted in at first available spot
Note:
Order of execution is explicitly not
preserved for asynchronous scripts
Runtime Techniques
Ways to ensure JavaScript doesn't run away
function processArray(items, process, callback){
    for (var i=0,len=items.length; i < len; i++){
        process(items[i]);
    }
    callback();
}
Technique #1: Timers
//create a new timer and delay by 500ms
 setTimeout(function(){


     //code to execute here


 }, 500)




setTimeout() schedules a function to be
  added to the UI queue after a delay
function timedProcessArray(items, process, callback){
    //create a clone of the original
    var todo = items.concat();
    setTimeout(function(){
        var start = +new Date();
        do {
              process(todo.shift());
        } while (todo.length > 0 &&
               (+new Date() - start < 50));
        if (todo.length > 0){
              setTimeout(arguments.callee, 25);
        } else {
              callback(items);
        }
    }, 25);
}
When Clicked

UI Thread



time
                           UI Queue

                             UI Update

                             onclick

                             UI Update
When Clicked

UI Thread

 UI Update

time
                            UI Queue

                              onclick

                              UI Update
When Clicked

UI Thread

 UI Update   onclick




time
                                  UI Queue

                                    UI Update
When Clicked

UI Thread

 UI Update   onclick   UI Update

time
                                   UI Queue
After 25ms

UI Thread

 UI Update   onclick   UI Update

time
                                    UI Queue

                                      JavaScript
After 25ms

UI Thread

 UI Update   onclick   UI Update   JavaScript




time
                                                UI Queue
After Another 25ms

UI Thread

 UI Update   onclick   UI Update   JavaScript




time
                                                UI Queue

                                                  JavaScript
After Another 25ms

UI Thread

 UI Update   onclick   UI Update   JavaScript         JavaScript




time
                                                UI Queue
Technique #2: Web Workers
Web Workers
â—Ź
  Asynchronous JavaScript execution
â—Ź
  Execution happens outside of UI thread
  â—Ź
    Not on the UI thread = no UI delays
â—Ź
  Data-driven API
  â—Ź
    Data is serialized when sending data into or
    out of Worker
  â—Ź
    No access to DOM, BOM
  â—Ź
    Completely separate execution environment
//in page
var worker = new Worker("process.js");
worker.onmessage = function(event){
     useData(event.data);
};
worker.postMessage(values);



//in process.js
self.onmessage = function(event){
     var items = event.data;
     for (var i=0,len=items.length; i < len; i++){
         process(items[i]);
     }
     self.postMessage(items);
};
When Clicked

UI Thread



time
                           UI Queue

                             UI Update

                             onclick

                             UI Update
When Clicked

UI Thread

 UI Update

time
                            UI Queue

                              onclick

                              UI Update
When Clicked

UI Thread

 UI Update   onclick




time
                                  UI Queue

                                    UI Update
When Clicked

UI Thread

 UI Update   onclick




time

             Worker Thread        UI Queue

                                    UI Update
When Clicked

UI Thread

 UI Update   onclick       UI Update

time

             Worker Thread             UI Queue

                       JavaScript
Worker Thread Complete

UI Thread

 UI Update    onclick   UI Update

time
                                    UI Queue

                                     onmessage
Worker Thread Complete

UI Thread

 UI Update    onclick   UI Update   onmessage




time
                                           UI Queue
Support for Web Workers




3.5 4.0    4.0      ?   10.6
Repaint and Reflow
Hidden performance costs of common operations
Long UI updates
       =
Unresponsive UI
Unresponsive UI

UI Thread

 UI Update   JavaScript   UI Update

time
JavaScript can cause
long UI updates by triggering
     repaint and reflow
A repaint occurs when a visual change doesn't
        require recalculation of layout
 Changes to visibility, colors (text/background), background images, etc.
Repaint
<button id="btn" style="font-size: 30px; padding: 0.5em
    1em">Click Me</button>

<script type="text/javascript">
window.onload = function(){
   document.getElementById("btn").onclick = function(){
       this.style.color = "#ff0";
   };
};
</script>                         Repaint!
A reflow occurs when a visual change
              requires a change in layout
Initial page load â–Ş browser resize â–Ş DOM structure change â–Ş layout style change
                          layout information retrieved
Reflow
<button id="btn" style="font-size: 30px; padding: 0.5em
    1em">Click Me</button>

<script type="text/javascript">
window.onload = function(){
   document.getElementById("btn").onclick = function(){
       var div = document.createElement(“div”);
       div.className = “tip”;
       div.innerHTML = “You clicked me!”;
       document.body.appendChild(div);
   };
};
</script>
                                Reflow!
Repaints and reflows are queued
   up as JavaScript executes
  and then executed in order
Reflow

var list = document.getElementsByClassName("items")[0],
    i, item;

for (i=0; i < 10; i++){
    item = document.createElement("li");
    item.innerHTML = "Item #" + i;
    list.appendChild(item);
}


                             Reflow x 10!
Limiting repaints/reflows
improves overall performance
Technique #1
Perform DOM manipulations
       off-document
Off-Document Operations
• Fast because there's no repaint/reflow
• Techniques:
   – Remove element from the document, make
     changes, insert back into document
   – Set element's display to “none”, make
     changes, set display back to default
   – Build up DOM changes on a
     DocumentFragment then apply all at once
DocumentFragment
• A document-like object
• Not visually represented
• Considered to be owned by the document from
  which it was created
• When passed to appendChild(), appends all
  of its children rather than itself
DocumentFragment

var list = document.getElementsByClassName("items")[0],
    fragment = document.createDocumentFragment(),
    i, item;

for (i=0; i < 10; i++){
    item = document.createElement("li");
    item.innerHTML = "Item #" + i;
    fragment.appendChild(item);
}

list.appendChild(fragment);



                         1 Reflow
Technique #2
Group Style Changes
Repaint!    Reflow!

element.style.color = "red";
element.style.height = "100px";
                                   Reflow!
element.style.fontSize = "25px";
element.style.backgroundColor = "white";



                               Repaint!
Grouping Style Changes

.active {
    color: red;
    height: 100px;
    fontSize: 25px;
    background-color: white;
}

element.className = "active";


                         Reflow!
Grouping Style Changes



var item = document.getElementById("myItem");
item.style.cssText = "color:red;height:100px;" +
             "font-size:25px;background-color: white");

                         Reflow!
Technique #3
Avoid Accidental Reflow
Accidental Reflow



element.width = "100px";

var width = element.offsetWidth;


                           Reflow!
What to do?
• Minimize access to layout information
  –   offsetTop, offsetLeft, offsetWidth, offsetHeight
  –   scrollTop, scrollLeft, scrollWidth, scrollHeight
  –   clientTop, clientLeft, clientWidth, clientHeight
  –   Most computed styles
• If a value is used more than once, store in
  local variable
Does hardware acceleration
          help?
Traditional Rendering

UI Thread

       Compositing   Drawing

time
Hardware Acceleration

UI Thread
 Prep                 Wait




time
        Rendering info            Signal complete



        Compositing     Drawing




              GPU
Recap
The browser UI thread is responsible for
both UI updates and JavaScript execution
           Only one can happen at a time
Responsive UI

UI Thread

 UI Update   JavaScript   UI Update

time
Unresponsive UI

UI Thread

 UI Update       JavaScript    UI Update

time
Unresponsive UI

UI Thread

 UI Update   JavaScript   UI Update

time
Avoid Slow Loading JavaScript
• Put scripts at the bottom
• Concatenate scripts into as few files as
  possible
• Choose the right way to load your scripts
   – Dynamically created scripts
   – Deferred scripts
   – Asynchronous scripts
Avoid Slow JavaScript
• Don't allow JavaScript to execute for more
  than 50ms
• Break up long JavaScript processes using:
   – Timers
   – Web Workers
Avoid Long UI Updates
• Be careful of repaint and reflow
• Perform complex DOM operations off-
  document
   – Remove elements and re-add them
   – Use DocumentFragment objects
• Group style changes together
• Avoid accidental reflow
Don't be horrible
Do be awesome
The End
Etcetera
• My blog:
  www.nczonline.net

• Twitter:
  @slicknet

• These Slides:
  http://slideshare.net/nzakas/
Creative Commons Images Used
•   http://www.flickr.com/photos/lrargerich/3115367361/
•   http://www.flickr.com/photos/hippie/2406411610/
•   http://www.flickr.com/photos/55733754@N00/3325000738/
•   http://www.flickr.com/photos/eurleif/255241547/
•   http://www.flickr.com/photos/off_the_wall/3444915939/
•   http://www.flickr.com/photos/wwarby/3296379139/
•   http://www.flickr.com/photos/derekgavey/4358797365/
•   http://www.flickr.com/photos/mulad/286641998/
•   http://www.flickr.com/photos/torley/2361164281/
•   http://www.flickr.com/photos/ottoman42/455242/
•   http://www.flickr.com/photos/goincase/3843348908/

Weitere ähnliche Inhalte

Was ist angesagt?

Mobile Web Speed Bumps
Mobile Web Speed BumpsMobile Web Speed Bumps
Mobile Web Speed BumpsNicholas Zakas
 
Responsive interfaces
Responsive interfacesResponsive interfaces
Responsive interfacesNicholas Zakas
 
JavaScript Timers, Power Consumption, and Performance
JavaScript Timers, Power Consumption, and PerformanceJavaScript Timers, Power Consumption, and Performance
JavaScript Timers, Power Consumption, and PerformanceNicholas Zakas
 
High Performance JavaScript - jQuery Conference SF Bay Area 2010
High Performance JavaScript - jQuery Conference SF Bay Area 2010High Performance JavaScript - jQuery Conference SF Bay Area 2010
High Performance JavaScript - jQuery Conference SF Bay Area 2010Nicholas Zakas
 
Nicholas' Performance Talk at Google
Nicholas' Performance Talk at GoogleNicholas' Performance Talk at Google
Nicholas' Performance Talk at GoogleNicholas Zakas
 
High Performance JavaScript (CapitolJS 2011)
High Performance JavaScript (CapitolJS 2011)High Performance JavaScript (CapitolJS 2011)
High Performance JavaScript (CapitolJS 2011)Nicholas Zakas
 
High Performance Snippets
High Performance SnippetsHigh Performance Snippets
High Performance SnippetsSteve Souders
 
Learning from the Best jQuery Plugins
Learning from the Best jQuery PluginsLearning from the Best jQuery Plugins
Learning from the Best jQuery PluginsMarc Grabanski
 
[jqconatx] Adaptive Images for Responsive Web Design
[jqconatx] Adaptive Images for Responsive Web Design[jqconatx] Adaptive Images for Responsive Web Design
[jqconatx] Adaptive Images for Responsive Web DesignChristopher Schmitt
 
What is HTML 5?
What is HTML 5?What is HTML 5?
What is HTML 5?Susan Winters
 
jQuery 1.9 and 2.0 - Present and Future
jQuery 1.9 and 2.0 - Present and FuturejQuery 1.9 and 2.0 - Present and Future
jQuery 1.9 and 2.0 - Present and FutureRichard Worth
 
Progressive Downloads and Rendering - take #2
Progressive Downloads and Rendering - take #2Progressive Downloads and Rendering - take #2
Progressive Downloads and Rendering - take #2Stoyan Stefanov
 
Testing Mobile JavaScript
Testing Mobile JavaScriptTesting Mobile JavaScript
Testing Mobile JavaScriptjeresig
 
Building performance into the new yahoo homepage presentation
Building performance into the new yahoo  homepage presentationBuilding performance into the new yahoo  homepage presentation
Building performance into the new yahoo homepage presentationmasudakram
 
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...David Kaneda
 
Don't make me wait! or Building High-Performance Web Applications
Don't make me wait! or Building High-Performance Web ApplicationsDon't make me wait! or Building High-Performance Web Applications
Don't make me wait! or Building High-Performance Web ApplicationsStoyan Stefanov
 
JavaScript Performance Patterns
JavaScript Performance PatternsJavaScript Performance Patterns
JavaScript Performance PatternsStoyan Stefanov
 
High Performance Social Plugins
High Performance Social PluginsHigh Performance Social Plugins
High Performance Social PluginsStoyan Stefanov
 

Was ist angesagt? (20)

Mobile Web Speed Bumps
Mobile Web Speed BumpsMobile Web Speed Bumps
Mobile Web Speed Bumps
 
Responsive interfaces
Responsive interfacesResponsive interfaces
Responsive interfaces
 
JavaScript Timers, Power Consumption, and Performance
JavaScript Timers, Power Consumption, and PerformanceJavaScript Timers, Power Consumption, and Performance
JavaScript Timers, Power Consumption, and Performance
 
High Performance JavaScript - jQuery Conference SF Bay Area 2010
High Performance JavaScript - jQuery Conference SF Bay Area 2010High Performance JavaScript - jQuery Conference SF Bay Area 2010
High Performance JavaScript - jQuery Conference SF Bay Area 2010
 
Nicholas' Performance Talk at Google
Nicholas' Performance Talk at GoogleNicholas' Performance Talk at Google
Nicholas' Performance Talk at Google
 
High Performance JavaScript (CapitolJS 2011)
High Performance JavaScript (CapitolJS 2011)High Performance JavaScript (CapitolJS 2011)
High Performance JavaScript (CapitolJS 2011)
 
High Performance Snippets
High Performance SnippetsHigh Performance Snippets
High Performance Snippets
 
Learning from the Best jQuery Plugins
Learning from the Best jQuery PluginsLearning from the Best jQuery Plugins
Learning from the Best jQuery Plugins
 
[jqconatx] Adaptive Images for Responsive Web Design
[jqconatx] Adaptive Images for Responsive Web Design[jqconatx] Adaptive Images for Responsive Web Design
[jqconatx] Adaptive Images for Responsive Web Design
 
What is HTML 5?
What is HTML 5?What is HTML 5?
What is HTML 5?
 
jQuery 1.9 and 2.0 - Present and Future
jQuery 1.9 and 2.0 - Present and FuturejQuery 1.9 and 2.0 - Present and Future
jQuery 1.9 and 2.0 - Present and Future
 
Cache is King
Cache is KingCache is King
Cache is King
 
Progressive Downloads and Rendering - take #2
Progressive Downloads and Rendering - take #2Progressive Downloads and Rendering - take #2
Progressive Downloads and Rendering - take #2
 
Testing Mobile JavaScript
Testing Mobile JavaScriptTesting Mobile JavaScript
Testing Mobile JavaScript
 
Building performance into the new yahoo homepage presentation
Building performance into the new yahoo  homepage presentationBuilding performance into the new yahoo  homepage presentation
Building performance into the new yahoo homepage presentation
 
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
 
Don't make me wait! or Building High-Performance Web Applications
Don't make me wait! or Building High-Performance Web ApplicationsDon't make me wait! or Building High-Performance Web Applications
Don't make me wait! or Building High-Performance Web Applications
 
JavaScript Performance Patterns
JavaScript Performance PatternsJavaScript Performance Patterns
JavaScript Performance Patterns
 
High Performance Social Plugins
High Performance Social PluginsHigh Performance Social Plugins
High Performance Social Plugins
 
Frontend SPOF
Frontend SPOFFrontend SPOF
Frontend SPOF
 

Andere mochten auch

Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)Nicholas Zakas
 
JavaScript Variable Performance
JavaScript Variable PerformanceJavaScript Variable Performance
JavaScript Variable PerformanceNicholas Zakas
 
Test Driven Development With YUI Test (Ajax Experience 2008)
Test Driven Development With YUI Test (Ajax Experience 2008)Test Driven Development With YUI Test (Ajax Experience 2008)
Test Driven Development With YUI Test (Ajax Experience 2008)Nicholas Zakas
 
Enterprise JavaScript Error Handling (Ajax Experience 2008)
Enterprise JavaScript Error Handling (Ajax Experience 2008)Enterprise JavaScript Error Handling (Ajax Experience 2008)
Enterprise JavaScript Error Handling (Ajax Experience 2008)Nicholas Zakas
 
High Performance JavaScript 2011
High Performance JavaScript 2011High Performance JavaScript 2011
High Performance JavaScript 2011Nicholas Zakas
 
Speed Up Your JavaScript
Speed Up Your JavaScriptSpeed Up Your JavaScript
Speed Up Your JavaScriptNicholas Zakas
 
Maintainable JavaScript 2012
Maintainable JavaScript 2012Maintainable JavaScript 2012
Maintainable JavaScript 2012Nicholas Zakas
 
Writing Efficient JavaScript
Writing Efficient JavaScriptWriting Efficient JavaScript
Writing Efficient JavaScriptNicholas Zakas
 

Andere mochten auch (8)

Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
 
JavaScript Variable Performance
JavaScript Variable PerformanceJavaScript Variable Performance
JavaScript Variable Performance
 
Test Driven Development With YUI Test (Ajax Experience 2008)
Test Driven Development With YUI Test (Ajax Experience 2008)Test Driven Development With YUI Test (Ajax Experience 2008)
Test Driven Development With YUI Test (Ajax Experience 2008)
 
Enterprise JavaScript Error Handling (Ajax Experience 2008)
Enterprise JavaScript Error Handling (Ajax Experience 2008)Enterprise JavaScript Error Handling (Ajax Experience 2008)
Enterprise JavaScript Error Handling (Ajax Experience 2008)
 
High Performance JavaScript 2011
High Performance JavaScript 2011High Performance JavaScript 2011
High Performance JavaScript 2011
 
Speed Up Your JavaScript
Speed Up Your JavaScriptSpeed Up Your JavaScript
Speed Up Your JavaScript
 
Maintainable JavaScript 2012
Maintainable JavaScript 2012Maintainable JavaScript 2012
Maintainable JavaScript 2012
 
Writing Efficient JavaScript
Writing Efficient JavaScriptWriting Efficient JavaScript
Writing Efficient JavaScript
 

Ă„hnlich wie High Performance JavaScript (Amazon DevCon 2011)

Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011Timothy Fisher
 
Developing High Performance Web Apps
Developing High Performance Web AppsDeveloping High Performance Web Apps
Developing High Performance Web AppsTimothy Fisher
 
Js Saturday 2013 your jQuery could perform better
Js Saturday 2013 your jQuery could perform betterJs Saturday 2013 your jQuery could perform better
Js Saturday 2013 your jQuery could perform betterIvo Andreev
 
Using HttpWatch Plug-in with Selenium Automation in Java
Using HttpWatch Plug-in with Selenium Automation in JavaUsing HttpWatch Plug-in with Selenium Automation in Java
Using HttpWatch Plug-in with Selenium Automation in JavaSandeep Tol
 
User Interface Patterns and Nuxeo
User Interface Patterns and NuxeoUser Interface Patterns and Nuxeo
User Interface Patterns and Nuxeoanicewick
 
Sanjeev ghai 12
Sanjeev ghai 12Sanjeev ghai 12
Sanjeev ghai 12Praveen kumar
 
Client side scripting using Javascript
Client side scripting using JavascriptClient side scripting using Javascript
Client side scripting using JavascriptBansari Shah
 
Play framework
Play frameworkPlay framework
Play frameworksambaochung
 
orcreatehappyusers
orcreatehappyusersorcreatehappyusers
orcreatehappyuserstutorialsruby
 
orcreatehappyusers
orcreatehappyusersorcreatehappyusers
orcreatehappyuserstutorialsruby
 
Setting UIAutomation free with Appium
Setting UIAutomation free with AppiumSetting UIAutomation free with Appium
Setting UIAutomation free with AppiumDan Cuellar
 
Hands on web development with play 2.0
Hands on web development with play 2.0Hands on web development with play 2.0
Hands on web development with play 2.0Abbas Raza
 
Introduction to JQuery, ASP.NET MVC and Silverlight
Introduction to JQuery, ASP.NET MVC and SilverlightIntroduction to JQuery, ASP.NET MVC and Silverlight
Introduction to JQuery, ASP.NET MVC and SilverlightPeter Gfader
 
[1C1]Service Workers
[1C1]Service Workers[1C1]Service Workers
[1C1]Service WorkersNAVER D2
 
Meebo performance ny_web_performance
Meebo performance ny_web_performanceMeebo performance ny_web_performance
Meebo performance ny_web_performancemarcuswestin
 
Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)Mikkel Flindt Heisterberg
 
Setting Apple's UI Automation Free with Appium
Setting Apple's UI Automation Free with AppiumSetting Apple's UI Automation Free with Appium
Setting Apple's UI Automation Free with Appiummobiletestsummit
 
JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)Steve Souders
 

Ă„hnlich wie High Performance JavaScript (Amazon DevCon 2011) (20)

Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011
 
Developing High Performance Web Apps
Developing High Performance Web AppsDeveloping High Performance Web Apps
Developing High Performance Web Apps
 
Js Saturday 2013 your jQuery could perform better
Js Saturday 2013 your jQuery could perform betterJs Saturday 2013 your jQuery could perform better
Js Saturday 2013 your jQuery could perform better
 
jQuery
jQueryjQuery
jQuery
 
Using HttpWatch Plug-in with Selenium Automation in Java
Using HttpWatch Plug-in with Selenium Automation in JavaUsing HttpWatch Plug-in with Selenium Automation in Java
Using HttpWatch Plug-in with Selenium Automation in Java
 
User Interface Patterns and Nuxeo
User Interface Patterns and NuxeoUser Interface Patterns and Nuxeo
User Interface Patterns and Nuxeo
 
Sanjeev ghai 12
Sanjeev ghai 12Sanjeev ghai 12
Sanjeev ghai 12
 
Client side scripting using Javascript
Client side scripting using JavascriptClient side scripting using Javascript
Client side scripting using Javascript
 
Play framework
Play frameworkPlay framework
Play framework
 
orcreatehappyusers
orcreatehappyusersorcreatehappyusers
orcreatehappyusers
 
orcreatehappyusers
orcreatehappyusersorcreatehappyusers
orcreatehappyusers
 
Setting UIAutomation free with Appium
Setting UIAutomation free with AppiumSetting UIAutomation free with Appium
Setting UIAutomation free with Appium
 
Webpack
Webpack Webpack
Webpack
 
Hands on web development with play 2.0
Hands on web development with play 2.0Hands on web development with play 2.0
Hands on web development with play 2.0
 
Introduction to JQuery, ASP.NET MVC and Silverlight
Introduction to JQuery, ASP.NET MVC and SilverlightIntroduction to JQuery, ASP.NET MVC and Silverlight
Introduction to JQuery, ASP.NET MVC and Silverlight
 
[1C1]Service Workers
[1C1]Service Workers[1C1]Service Workers
[1C1]Service Workers
 
Meebo performance ny_web_performance
Meebo performance ny_web_performanceMeebo performance ny_web_performance
Meebo performance ny_web_performance
 
Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)
 
Setting Apple's UI Automation Free with Appium
Setting Apple's UI Automation Free with AppiumSetting Apple's UI Automation Free with Appium
Setting Apple's UI Automation Free with Appium
 
JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)JavaScript Performance (at SFJS)
JavaScript Performance (at SFJS)
 

Mehr von Nicholas Zakas

The Pointerless Web
The Pointerless WebThe Pointerless Web
The Pointerless WebNicholas Zakas
 
JavaScript APIs you’ve never heard of (and some you have)
JavaScript APIs you’ve never heard of (and some you have)JavaScript APIs you’ve never heard of (and some you have)
JavaScript APIs you’ve never heard of (and some you have)Nicholas Zakas
 
Scalable JavaScript Application Architecture 2012
Scalable JavaScript Application Architecture 2012Scalable JavaScript Application Architecture 2012
Scalable JavaScript Application Architecture 2012Nicholas Zakas
 
Maintainable JavaScript 2011
Maintainable JavaScript 2011Maintainable JavaScript 2011
Maintainable JavaScript 2011Nicholas Zakas
 
Scalable JavaScript Application Architecture
Scalable JavaScript Application ArchitectureScalable JavaScript Application Architecture
Scalable JavaScript Application ArchitectureNicholas Zakas
 
Extreme JavaScript Compression With YUI Compressor
Extreme JavaScript Compression With YUI CompressorExtreme JavaScript Compression With YUI Compressor
Extreme JavaScript Compression With YUI CompressorNicholas Zakas
 
Maintainable JavaScript
Maintainable JavaScriptMaintainable JavaScript
Maintainable JavaScriptNicholas Zakas
 
The New Yahoo! Homepage and YUI 3
The New Yahoo! Homepage and YUI 3The New Yahoo! Homepage and YUI 3
The New Yahoo! Homepage and YUI 3Nicholas Zakas
 

Mehr von Nicholas Zakas (8)

The Pointerless Web
The Pointerless WebThe Pointerless Web
The Pointerless Web
 
JavaScript APIs you’ve never heard of (and some you have)
JavaScript APIs you’ve never heard of (and some you have)JavaScript APIs you’ve never heard of (and some you have)
JavaScript APIs you’ve never heard of (and some you have)
 
Scalable JavaScript Application Architecture 2012
Scalable JavaScript Application Architecture 2012Scalable JavaScript Application Architecture 2012
Scalable JavaScript Application Architecture 2012
 
Maintainable JavaScript 2011
Maintainable JavaScript 2011Maintainable JavaScript 2011
Maintainable JavaScript 2011
 
Scalable JavaScript Application Architecture
Scalable JavaScript Application ArchitectureScalable JavaScript Application Architecture
Scalable JavaScript Application Architecture
 
Extreme JavaScript Compression With YUI Compressor
Extreme JavaScript Compression With YUI CompressorExtreme JavaScript Compression With YUI Compressor
Extreme JavaScript Compression With YUI Compressor
 
Maintainable JavaScript
Maintainable JavaScriptMaintainable JavaScript
Maintainable JavaScript
 
The New Yahoo! Homepage and YUI 3
The New Yahoo! Homepage and YUI 3The New Yahoo! Homepage and YUI 3
The New Yahoo! Homepage and YUI 3
 

KĂĽrzlich hochgeladen

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

KĂĽrzlich hochgeladen (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure service
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

High Performance JavaScript (Amazon DevCon 2011)

  • 1. High Performance JavaScript Nicholas C. Zakas | Amazon DevCon | April 27, 2011
  • 2. Who's this guy? Presentation Contributor, Architect Creator of YUI Test Author Lead Author Contributor Lead Author
  • 3.
  • 5.
  • 7. After all, all browsers now have optimizing JavaScript engines Tracemonkey/ V8 Squirrelfish Chakra Karakan JaegarMonkey (all) (4+) (9+) (10.5+) (3.5+)
  • 8. So our scripts are getting really, really fast
  • 9. Old computers ran slow applications Small amounts of CPU power and memory
  • 10. New computers are generally faster but slow applications still exist More CPU + more memory = less disciplined application development
  • 12.
  • 13.
  • 14. Oh yeah, one more thing
  • 15.
  • 17. It's still possible to write slow JavaScript on the new, faster JavaScript engines
  • 18. JavaScript performance directly affects user experience
  • 19. "Know the enemy and know yourself; in a hundred battles you will never be in peril." -Sun Tzu, The Art of War
  • 20. The UI Thread The brains of the operation
  • 21. The browser UI thread is responsible for both UI updates and JavaScript execution Only one can happen at a time
  • 22. Jobs for UI updates and JavaScript execution are added to a UI queue Each job must wait in line for its turn to execute
  • 23. <button id="btn" style="font-size: 30px; padding: 0.5em 1em">Click Me</button> <script type="text/javascript"> window.onload = function(){ document.getElementById("btn").onclick = function(){ //do something }; }; </script>
  • 25. When Clicked UI Thread time UI Queue UI Update onclick UI Update
  • 26. When Clicked UI Thread UI Update time UI Queue onclick Draw down state UI Update
  • 27. When Clicked UI Thread UI Update onclick time UI Queue UI Update
  • 28. When Clicked UI Thread UI Update onclick UI Update time UI Queue Draw up state
  • 29. No UI updates while JavaScript is executing
  • 30. JavaScript May Cause UI Update <button id="btn" style="font-size: 30px; padding: 0.5em 1em">Click Me</button> <script type="text/javascript"> window.onload = function(){ document.getElementById("btn").onclick = function(){ var div = document.createElement(“div”); div.className = “tip”; div.innerHTML = “You clicked me!”; document.body.appendChild(div); }; }; </script>
  • 31. A UI update must use the latest info available
  • 32. Long-running JavaScript = Unresponsive UI
  • 33. Responsive UI UI Thread UI Update JavaScript UI Update time
  • 34. Unresponsive UI UI Thread UI Update JavaScript UI Update time
  • 35. The longer JavaScript runs, the worse the user experience
  • 36. The runaway script timer prevents JavaScript from running for too long Each browser imposes its own limit (except Opera)
  • 41.
  • 42. Runaway Script Timer Limits • Internet Explorer: 5 million statements • Firefox: 10 seconds • Safari: 5 seconds • Chrome: Unknown, hooks into normal crash control mechanism • Opera: none
  • 45. JITed JavaScript (1st Run) UI Thread Compile Execute time
  • 46. JITed JavaScript (After 1st Run) UI Thread Execute time
  • 47. How Long Is Too Long? “0.1 second [100ms] is about the limit for having the user feel that the system is reacting instantaneously, meaning that no special feedback is necessary except to display the result.” - Jakob Nielsen
  • 48. Translation: No single JavaScript job should execute for more than 100ms to ensure a responsive UI
  • 49. Recommendation: Limit JavaScript execution to no more than 50ms measured on IE6 :)
  • 50. Doing so makes your application awesome
  • 51. Loadtime Techniques Don't let JavaScript interfere with page load performance
  • 52. During page load, JavaScript takes more time on the UI thread
  • 53. <!doctype html> <html> <head> <title>Example</title> </head> <body> <p>Hello world!</p> <script src="foo.js"></script> <p>See ya!</p> </body> </html>
  • 54. Result UI Thread UI Update JavaScript UI Update time
  • 55. Result UI Thread Hello world! foo.js See ya! time
  • 56. Result UI Thread Hello world! Download Parse Run See ya! time The UI thread needs to wait for the script to download, parse, and run before continuing
  • 57. Result UI Thread Hello world! Download Parse Run See ya! Variable Constant Download time takes the longest and is variable
  • 58. Translation: The page doesn't render while JavaScript is downloading, parsing, or executing during page load
  • 59. <!doctype html> <html> <head> <title>Example</title> </head> <body> <script src="foo.js"></script> <p>Hello world!</p> <script src="bar.js"></script> <p>See ya!</p> <script src="baz.js"></script> <p>Uh oh!</p> </body> </html>
  • 60. Result UI Thread JavaScript UI Update JavaScript UI Update JavaScript time The more scripts to download in between UI updates, the longer the page takes to render
  • 61. Technique #1: Put scripts at the bottom
  • 62.
  • 63. <!doctype html> <html> <head> <title>Example</title> </head> <body> <p>Hello world!</p> <p>See ya!</p> <script src="foo.js"></script> </body> </html>
  • 64. Put Scripts at Bottom UI Thread UI Update UI Update JavaScript JavaScript JavaScript time Even if there are multiple scripts, the page renders quickly
  • 65. Technique #2: Combine JavaScript files
  • 66. <!doctype html> <html> <head> <title>Example</title> </head> <body> <p>Hello world!</p> <p>See ya!</p> <script src="foo.js"></script> <script src="bar.js"></script> <script src="baz.js"></script> </body> </html>
  • 67. UI Thread UI Update JavaScript JavaScript JavaScript time Each script has overhead of downloading
  • 68. UI Thread UI Update JavaScript time Combining all of the files limits the network overhead and gets scripts onto the page faster
  • 69. <!doctype html> <html> <head> <title>Example</title> </head> <body> <p>Hello world!</p> <p>See ya!</p> <script src="foo-and-bar-and-baz.js"></script> </body> </html>
  • 70. Technique #3: Load scripts dynamically
  • 71. Basic Technique var script = document.createElement("script"), body; script.type = "text/javascript"; script.src = "foo.js"; body.insertBefore(script, body.firstChild); Dynamically loaded scripts are non-blocking
  • 72. Downloads no longer block the UI thread
  • 73. <!doctype html> <html> <head> <title>Example</title> </head> <body> <p>Hello world!</p> <script src="foo.js"></script> <p>See ya!</p> </body> </html>
  • 74. Using HTML <script> UI Thread Hello world! Download Parse Run See ya! time
  • 75. <!doctype html> <html> <head> <title>Example</title> </head> <body> <p>Hello world!</p> <script> var script = document.createElement("script"), body = document.body; script.type = "text/javascript"; script.src = "foo.js"; body.insertBefore(script, body.firstChild); </script> <p>See ya!</p><!-- more content --> </body> </html>
  • 76. Using Dynamic Scripts UI Thread Hello world! See ya! Run UI Update time Download Parse Only code execution happens on the UI thread, which means less blocking of UI updates
  • 77. function loadScript(url, callback){ var script = document.createElement("script"), body = document.body; script.type = "text/javascript"; if (script.readyState){ //IE <= 8 script.onreadystatechange = function(){ if (script.readyState == "loaded" || script.readyState == "complete"){ script.onreadystatechange = null; callback(); } }; } else { //Others script.onload = function(){ callback(); }; } script.src = url; body.insertBefore(script, body.firstChild); }
  • 78. Usage loadScript("foo.js", function(){ alert("Loaded!"); });
  • 79. Timing Note: Script execution begins immediately after download and parse – timing of execution is not guaranteed
  • 80. Using Dynamic Scripts UI Thread Hello world! Run See ya! UI Update time Download Parse Depending on time to download and script size, execution may happen before next UI update
  • 82. <!doctype html> <html> <head> <title>Example</title> </head> <body> <p>Hello world!</p> <script defer src="foo.js"></script> <p>See ya!</p> <!-- even more markup --> </body> </html>
  • 83. Support for <script defer> 3.5 7.0 5.0 4.0 ?
  • 84. Deferred scripts begin to download immediately, but don't execute until all UI updates complete
  • 85. Using <script defer> UI Thread Hello world! See ya! More UI More UI Run time Download Parse Similar to dynamic script nodes, but with a guarantee that execution will happen last
  • 86. Timing Note: Although scripts always execute after UI updates complete, the order of multiple <script defer> scripts is not guaranteed across browsers
  • 88. <!doctype html> <html> <head> <title>Example</title> </head> <body> <p>Hello world!</p> <script async src="foo.js"></script> <p>See ya!</p> <!-- even more markup --> </body> </html>
  • 89. Support for <script async> 3.6 7.0 5.0 ? ?
  • 90. Asynchronous scripts behave a lot like dynamic scripts
  • 91. Using <script async> UI Thread Hello world! See ya! Run UI Update time Download Parse Download begins immediately and execution is slotted in at first available spot
  • 92. Note: Order of execution is explicitly not preserved for asynchronous scripts
  • 93. Runtime Techniques Ways to ensure JavaScript doesn't run away
  • 94. function processArray(items, process, callback){ for (var i=0,len=items.length; i < len; i++){ process(items[i]); } callback(); }
  • 96. //create a new timer and delay by 500ms setTimeout(function(){ //code to execute here }, 500) setTimeout() schedules a function to be added to the UI queue after a delay
  • 97. function timedProcessArray(items, process, callback){ //create a clone of the original var todo = items.concat(); setTimeout(function(){ var start = +new Date(); do { process(todo.shift()); } while (todo.length > 0 && (+new Date() - start < 50)); if (todo.length > 0){ setTimeout(arguments.callee, 25); } else { callback(items); } }, 25); }
  • 98. When Clicked UI Thread time UI Queue UI Update onclick UI Update
  • 99. When Clicked UI Thread UI Update time UI Queue onclick UI Update
  • 100. When Clicked UI Thread UI Update onclick time UI Queue UI Update
  • 101. When Clicked UI Thread UI Update onclick UI Update time UI Queue
  • 102. After 25ms UI Thread UI Update onclick UI Update time UI Queue JavaScript
  • 103. After 25ms UI Thread UI Update onclick UI Update JavaScript time UI Queue
  • 104. After Another 25ms UI Thread UI Update onclick UI Update JavaScript time UI Queue JavaScript
  • 105. After Another 25ms UI Thread UI Update onclick UI Update JavaScript JavaScript time UI Queue
  • 106. Technique #2: Web Workers
  • 107.
  • 108. Web Workers â—Ź Asynchronous JavaScript execution â—Ź Execution happens outside of UI thread â—Ź Not on the UI thread = no UI delays â—Ź Data-driven API â—Ź Data is serialized when sending data into or out of Worker â—Ź No access to DOM, BOM â—Ź Completely separate execution environment
  • 109. //in page var worker = new Worker("process.js"); worker.onmessage = function(event){ useData(event.data); }; worker.postMessage(values); //in process.js self.onmessage = function(event){ var items = event.data; for (var i=0,len=items.length; i < len; i++){ process(items[i]); } self.postMessage(items); };
  • 110. When Clicked UI Thread time UI Queue UI Update onclick UI Update
  • 111. When Clicked UI Thread UI Update time UI Queue onclick UI Update
  • 112. When Clicked UI Thread UI Update onclick time UI Queue UI Update
  • 113. When Clicked UI Thread UI Update onclick time Worker Thread UI Queue UI Update
  • 114. When Clicked UI Thread UI Update onclick UI Update time Worker Thread UI Queue JavaScript
  • 115. Worker Thread Complete UI Thread UI Update onclick UI Update time UI Queue onmessage
  • 116. Worker Thread Complete UI Thread UI Update onclick UI Update onmessage time UI Queue
  • 117. Support for Web Workers 3.5 4.0 4.0 ? 10.6
  • 118. Repaint and Reflow Hidden performance costs of common operations
  • 119. Long UI updates = Unresponsive UI
  • 120. Unresponsive UI UI Thread UI Update JavaScript UI Update time
  • 121. JavaScript can cause long UI updates by triggering repaint and reflow
  • 122. A repaint occurs when a visual change doesn't require recalculation of layout Changes to visibility, colors (text/background), background images, etc.
  • 123. Repaint <button id="btn" style="font-size: 30px; padding: 0.5em 1em">Click Me</button> <script type="text/javascript"> window.onload = function(){ document.getElementById("btn").onclick = function(){ this.style.color = "#ff0"; }; }; </script> Repaint!
  • 124. A reflow occurs when a visual change requires a change in layout Initial page load â–Ş browser resize â–Ş DOM structure change â–Ş layout style change layout information retrieved
  • 125. Reflow <button id="btn" style="font-size: 30px; padding: 0.5em 1em">Click Me</button> <script type="text/javascript"> window.onload = function(){ document.getElementById("btn").onclick = function(){ var div = document.createElement(“div”); div.className = “tip”; div.innerHTML = “You clicked me!”; document.body.appendChild(div); }; }; </script> Reflow!
  • 126. Repaints and reflows are queued up as JavaScript executes and then executed in order
  • 127. Reflow var list = document.getElementsByClassName("items")[0], i, item; for (i=0; i < 10; i++){ item = document.createElement("li"); item.innerHTML = "Item #" + i; list.appendChild(item); } Reflow x 10!
  • 129. Technique #1 Perform DOM manipulations off-document
  • 130. Off-Document Operations • Fast because there's no repaint/reflow • Techniques: – Remove element from the document, make changes, insert back into document – Set element's display to “none”, make changes, set display back to default – Build up DOM changes on a DocumentFragment then apply all at once
  • 131. DocumentFragment • A document-like object • Not visually represented • Considered to be owned by the document from which it was created • When passed to appendChild(), appends all of its children rather than itself
  • 132. DocumentFragment var list = document.getElementsByClassName("items")[0], fragment = document.createDocumentFragment(), i, item; for (i=0; i < 10; i++){ item = document.createElement("li"); item.innerHTML = "Item #" + i; fragment.appendChild(item); } list.appendChild(fragment); 1 Reflow
  • 134. Repaint! Reflow! element.style.color = "red"; element.style.height = "100px"; Reflow! element.style.fontSize = "25px"; element.style.backgroundColor = "white"; Repaint!
  • 135. Grouping Style Changes .active { color: red; height: 100px; fontSize: 25px; background-color: white; } element.className = "active"; Reflow!
  • 136. Grouping Style Changes var item = document.getElementById("myItem"); item.style.cssText = "color:red;height:100px;" + "font-size:25px;background-color: white"); Reflow!
  • 138. Accidental Reflow element.width = "100px"; var width = element.offsetWidth; Reflow!
  • 139. What to do? • Minimize access to layout information – offsetTop, offsetLeft, offsetWidth, offsetHeight – scrollTop, scrollLeft, scrollWidth, scrollHeight – clientTop, clientLeft, clientWidth, clientHeight – Most computed styles • If a value is used more than once, store in local variable
  • 141. Traditional Rendering UI Thread Compositing Drawing time
  • 142. Hardware Acceleration UI Thread Prep Wait time Rendering info Signal complete Compositing Drawing GPU
  • 143. Recap
  • 144. The browser UI thread is responsible for both UI updates and JavaScript execution Only one can happen at a time
  • 145. Responsive UI UI Thread UI Update JavaScript UI Update time
  • 146. Unresponsive UI UI Thread UI Update JavaScript UI Update time
  • 147. Unresponsive UI UI Thread UI Update JavaScript UI Update time
  • 148. Avoid Slow Loading JavaScript • Put scripts at the bottom • Concatenate scripts into as few files as possible • Choose the right way to load your scripts – Dynamically created scripts – Deferred scripts – Asynchronous scripts
  • 149. Avoid Slow JavaScript • Don't allow JavaScript to execute for more than 50ms • Break up long JavaScript processes using: – Timers – Web Workers
  • 150. Avoid Long UI Updates • Be careful of repaint and reflow • Perform complex DOM operations off- document – Remove elements and re-add them – Use DocumentFragment objects • Group style changes together • Avoid accidental reflow
  • 154. Etcetera • My blog: www.nczonline.net • Twitter: @slicknet • These Slides: http://slideshare.net/nzakas/
  • 155. Creative Commons Images Used • http://www.flickr.com/photos/lrargerich/3115367361/ • http://www.flickr.com/photos/hippie/2406411610/ • http://www.flickr.com/photos/55733754@N00/3325000738/ • http://www.flickr.com/photos/eurleif/255241547/ • http://www.flickr.com/photos/off_the_wall/3444915939/ • http://www.flickr.com/photos/wwarby/3296379139/ • http://www.flickr.com/photos/derekgavey/4358797365/ • http://www.flickr.com/photos/mulad/286641998/ • http://www.flickr.com/photos/torley/2361164281/ • http://www.flickr.com/photos/ottoman42/455242/ • http://www.flickr.com/photos/goincase/3843348908/