JavaScript: The Good Parts Part Six: Ajax Performance Douglas Crockford

Slides:



Advertisements
Similar presentations
1. XP 2 * The Web is a collection of files that reside on computers, called Web servers. * Web servers are connected to each other through the Internet.
Advertisements

Zhongxing Telecom Pakistan (Pvt.) Ltd
JavaScript: A Language of Many Contrasts
Chung for Robofest 05 1 Introduction to RoboLab CJ Chung Lawrence Technological University.
Introduction to HTML, XHTML, and CSS
Code Tuning Strategies and Techniques
1 Real World Performance Tuning Ask Bjørn Hansen OSCON 2001.
Database Performance Tuning and Query Optimization
DB Relay An Introduction. INSPIRATION Database access is WAY TOO HARD The crux.
Javascript Code Quality Check Tools Javascript Code Quality Check Tools JavaScript was originally intended to do small tasks in webpages, but now JavaScript.
XP New Perspectives on Browser and Basics Tutorial 1 1 Browser and Basics Tutorial 1.
PHP Reusing Code and Writing Functions.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
ASP.NET Best Practices Dawit Wubshet Park University.
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
Copyright © 2003, SAS Institute Inc. All rights reserved. Where's Waldo Uncovering Hard-to-Find Application Killers Claire Cates SAS Institute, Inc
JSON, ADsafe, and Misty Douglas Crockford Yahoo!.
National Instruments Confidential. 2 LabVIEW Development Tips and Tricks Revealed Carl Ljungholm - LabVIEW Product Support Engineer Travis Hailey - LabVIEW.
WHAT IS AJAX? Zack Sheppard [zts2101] WHIM April 19, 2011.
27-Jun-15 Profiling code, Timing Methods. Optimization Optimization is the process of making a program as fast (or as small) as possible Here’s what the.
Chapter 3.1 Teams and Processes. 2 Programming Teams In the 1980s programmers developed the whole game (and did the art and sounds too!) Now programmers.
30-Jun-15 Profiling. Optimization Optimization is the process of making a program as fast (or as small) as possible Here’s what the experts say about.
Code Generation CS 480. Can be complex To do a good job of teaching about code generation I could easily spend ten weeks But, don’t have ten weeks, so.
SM3121 Software Technology Mark Green School of Creative Media.
Performance and Code Tuning Overview CPSC 315 – Programming Studio Fall 2009.
Chapter Ten Performance Tuning. Objectives Create a performance baseline Create a performance baseline Understand the performance and monitoring tools.
Virtual Memory Tuning   You can improve a server’s performance by optimizing the way the paging file is used   You may want to size the paging file.
JavaScript & Metaperformance Douglas Crockford Yahoo! Inc.
An Introduction to Content Management. By the end of the session you will be able to... Explain what a content management system is Apply the principles.
Software Construction. Implementation System Specification Requirements Analysis Architectural Design Detailed Design Coding & Debugging Unit Testing.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
 Zhichun Li  The Robust and Secure Systems group at NEC Research Labs  Northwestern University  Tsinghua University 2.
Ruth Betcher Ruth Christie
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
Advanced Web Design Scripting Tutorial Chapters. Scripting Intro The scripting part of the forthcoming Advanced Web Design textbook introduces you to.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Performance Concepts Mark A. Magumba. Introduction Research done on 1058 correspondents in 2006 found that 75% OF them would not return to a website that.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Database-Driven Web Sites, Second Edition1 Chapter 5 WEB SERVERS.
Cross Site Integration “mashups” cross site scripting.
1 ENERGY 211 / CME 211 Lecture 26 November 19, 2008.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
AjaxScope & Doloto: Towards Optimizing Client-side Web 2.0 App Performance Ben Livshits Microsoft Research (joint work with Emre.
Building Rich Web Applications with Ajax Linda Dailey Paulson IEEE – Computer, October 05 (Vol.38, No.10) Presented by Jingming Zhang.
Debugging and Profiling With some help from Software Carpentry resources.
PERFORMANCE ENHANCEMENT IN ASP.NET By Hassan Tariq Session #1.
Welcome to jQuery jQuery is one of many available libraries that – Provide functions for manipulating the web page With fairly good performance – Help.
JSON – Java Script Object Notation. What is JSON JSON is a data interchange format Interactive Web 2.0 applications, no more use page replacement. Data.
 Web pages originally static  Page is delivered exactly as stored on server  Same information displayed for all users, from all contexts  Dynamic.
Creating Animations, Working with Graphics, and Accessing Data Lesson 9.
Web Technologies Lecture 8 JQuery. “A fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Web Technology (NCS-504) Prepared By Mr. Abhishek Kesharwani Assistant Professor,UCER Naini,Allahabad.
AJAX CS456 Fall Examples Where is AJAX used? Why do we care?
JQUERY AND AJAX
WHAT'S THE DIFFERENCE BETWEEN A WEB APPLICATION STREAMING NETWORK AND A CDN? INSTART LOGIC.
Lecture 1 Page 1 CS 111 Summer 2013 Important OS Properties For real operating systems built and used by real people Differs depending on who you are talking.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
Chapter 2: System Structures
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
Optimize Your Java Code By Tools
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
Introduction to AJAX and JSON
Introduction to JavaScript
Yale Digital Conference 2019
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
Presentation transcript:

JavaScript: The Good Parts Part Six: Ajax Performance Douglas Crockford

Memento

The Sessionless Web Cookies for pseudosessions. Cookies enable CSRF attacks. Every action results in a page replacement. Pages are heavy, complicated, multipart things. The web is a big step backwards in individual productivity.

When your only tool is a hammer, every problem looks like a webpage.

The Ajax Revolution The page is an application with a data connection to a server.

When the user does something, we send a JSON message to the server, and receive a JSON message as the result.

A JSON message is less work for the server to generate, moves faster on the wire, and is less work for the browser to parse and render than an HTML document.

Division of Labor How is the application divided between the browser and the server?

Pendulum of Despair Server The browser is a terminal.

Pendulum of Despair Server Browser The browser is a terminal The server is a file system.

Seek the Middle Way. A pleasant dialogue between specialized peers.

Ajaxify The client and server are in a dialog. Make the messages between them as small as possible. The client does not need a copy of the database. It just needs, at any moment, just enough information to serve the user. Don't rewrite the server application in JavaScript!

The Poor Browser The browser is a very inefficient application platform. If your application becomes bloated, performance will be very bad. Try to keep the client programming light.

Amazingly, the browser works But it doesn't work well. It is a difficult platform to work with. There are significant security problems. There are significant performance problems. It was not designed to be an application delivery system. Ajax pushes the browser really hard.

Correctness First Do not worry about optimization until you have the application working correctly. If it isnt right, it doesnt matter that if it is fast. Test for performance early. Test in customer configurations: Slow networks, slow computers. Internal networks and developer-class machines can mask performance problems.

Premature optimization is the root of all evil. Donald Knuth Use YSlow to reduce startup time. Don't optimize until you need to, but find out as early as possible if you need to. Clean, correct code is easier to optimize. Tweaking is usually ineffective. Sometimes restructuring or redesign is required.

Example var fibonacci = function (n) { return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2); }; fibonacci(40) Calls itself 331,160,280 times.

Memoizer var memoizer = function (memo, fundamental) { var shell = function (n) { var result = memo[n]; if (typeof result !== 'number') { result = fundamental(shell, n); memo[n] = result; } return result; }; return shell; };

Memoizer var fibonacci = memoizer([0, 1], function (recur, n) { return recur(n - 1) + recur(n - 2); }); fibonacci(40) Calls itself 38 times. The key to optimization is work avoidance.

Code Quality High quality code is mostly likely to avoid platform problems. Code Conventions for the JavaScript Programming Language Use JSLint.com. Pass with no warnings.

Have regular code readings. Dont wait until release to do code reviews. Do team code reading regularly during development. Experienced developers can lead by example. Novice developers learn from the group. Problems can be discovered early. Good techniques can be shared early.

Two Kinds of Optimization Streamlining Algorithm replacement Work avoidance Code removal These things are always good to do Special Casing Adds cruft, increases code size Should only be done when proven necessary

Avoid unnecessary displays or animation. Everything costs. Wow costs. As the number of widgets on the page increases, overall ongoing performance gets worse.

Only speed up things that take a lot of time. Speeding up things that take very little time will yield very little improvement.

Only speed up things that take a lot of time. If profiling shows that you are spending most of your time in A, don't bother optimizing C. ABCD

Improving performance If JavaScript were infinitely fast, most pages would run at about the same speed. The bottleneck tends to be the DOM interface. There is a significant cost every time you touch the DOM tree. Each touch can result in a reflow computation, which is expensive.

Touch lightly It is faster to manipulate new nodes before they are attached to the tree. Touching unattached nodes avoids the reflow cost. Setting innerHTML does an enormous amount of work, but browsers are really good at it, and it only touches the DOM once.

Make good use of Ajax Libraries Effective code reuse will make widgets more effective.

How IE8 Spends Its Time Average time allocation of the Alexa 100: Layout 43.16% Rendering 27.25% HTML 2.81% Marshalling 7.34% DOM 5.05% Format 8.66% JScript 3.23% Other 2.5%

How IE8 Spends Its Time Opening a thread in GMail: Layout 9.41% Rendering 9.21% HTML 1.57% Marshalling 7.85% DOM 12.47% Format 38.97% JScript 14.43% Other 3.72%

Coding Efficiency Common subexpression removal. Loop invariant removal. Most compilers in most programming languages do these optimizations for you. But not JavaScript.

Before var i; for (i = 0; i < divs.length; i += 1) { divs[i].style.color = "black"; divs[i].style.border = thickness + 'px solid blue'; divs[i].style.backgroundColor = "white"; }

After var border = thickness + 'px solid blue', nrDivs = divs.length, ds, i; for (i = 0; i < nrDivs; i += 1) { ds = divs[i].style; ds.color = "black"; ds.border = border; ds.backgroundColor = "white"; }

Strings Concatenation with + Each operation allocates memory foo = a + b; Concatenate with array.join('') The contents of an array are concatenated into a single string foo = [a, b].join('');

Don't Tune For Quirks Some browsers have surprising inefficiencies. A trick that is faster on Browser A might be slower on Browser B. The performance characteristics of the next generation may be significantly different. Avoid short-term optimizations.

Don't Optimize Without Measuring Intuitions are often wrong. start_time = new Date().valueOf(); code_to_measured(); end_time = new Date().valueOf(); elapsed_time = end_time - start_time; A single trial is unreliable. Timers can be off by as much as 15 msec. Even accurate measurements can lead to wrong conclusions.

O (1) An operation that is performed only once is not worth optimizing.

O (n) An operation that is performed many times may be worth optimizing. Time n Slope: Time per Iteration Fixed time: Startup and cleanup

The Axis of Error Inefficiency Time n Inefficiency

The Axis of Error Frustration Time n Frustration

The Axis of Error Failure Time n Failure

O (n) Time n Slope: Time per Iteration Fixed time: Startup and cleanup

O (n log n) Time n

O (n 2 ) Generally not suitable for browser applications except when n is very small. Time n

The most effective way to make programs faster is to make n smaller. Ajax allows for just-in-time data delivery.

The Wall.