performancetuning ajax applications

35
Performance Tune your Ajax Applications The Ajax Experience 2007 Bob Buffone

Upload: bob-buffone

Post on 12-Nov-2014

652 views

Category:

Documents


2 download

DESCRIPTION

Power point defines ways to optimize your JavaScript applications.

TRANSCRIPT

Page 1: PerformanceTuning Ajax Applications

Performance Tune your Ajax

Applications

The Ajax Experience 2007

Bob Buffone

Page 2: PerformanceTuning Ajax Applications

Agenda

• Introduction

• Performance Tuning

– Startup Time

– Runtime

• Metrics

• jsLex

Page 3: PerformanceTuning Ajax Applications

Introduction

• Name: Bob Buffone

• Company: Nexaweb Technologies, Inc.

• Position: Chief Architect

• Other Projects:

– Apache XAP (committer)

– Random Mashups

– jsLex

– Just like to make things small and fast

Page 4: PerformanceTuning Ajax Applications

Startup Time

• # of Requests

• Size of Requests

• Time of Requests

• Time of initial code completion

Page 5: PerformanceTuning Ajax Applications

Reducing # of Requests

• Highly important for applications that use lots of JavaScript files.

– Even when cached the browser still makes a request

Page 6: PerformanceTuning Ajax Applications

Tips to Reduce # of Requests

• Condense files into one or more larger files

– Runtime

– Development

file_a.jsfile_a.js

file_b.jsfile_b.js

file_c.js =file_a.js + file_b.js

file_c.js =file_a.js + file_b.jsConcatenationConcatenation

Page 7: PerformanceTuning Ajax Applications

Tips to Reduce # of Requests

• Server-side code combines files based on a request.

file_a.jsfile_a.js

file_b.jsfile_b.js

File_c.js =

file_a.js + file_b.js

File_c.js =

file_a.js + file_b.js

PHP/JSP/

Ruby

PHP/JSP/

Ruby

Page 8: PerformanceTuning Ajax Applications

Server-side Concatenation

• Pluses:

– Easy depending on your server tier

– Easy to update code

• Minuses:

– Performance load on server.

Can be mitigated with caching

– Hard to distribute code to other people

• Where it could be used:

– Small single projects

Page 9: PerformanceTuning Ajax Applications

Tips to Reduce # of Requests

• PHP Example:

• <script src=“combiner.php”> </script>

function combineFile($file){

$handle = fopen($file, "r");

if ($handle != null){

echo fread($handle, filesize($file));

}

}

//combine files

combineFile(“file_a.js");

combineFile(“file_b.js");

Page 10: PerformanceTuning Ajax Applications

Tips to Reduce # of Requests

• Development and Build Processes

– Dojo

– Ant

– Command line scripts

file_a.jsfile_a.js

file_b.jsfile_b.js

file_c.js =

file_a.js+file_b.js

file_c.js =

file_a.js+file_b.js

Ant /

Dojo

Ant /

Dojo

Page 11: PerformanceTuning Ajax Applications

Dojo Advantages / Disadvantages• Pluses:

– Not server load issues

– Easy to update code

– Dependency based

• Minuses:– More Complicated (Ant, Rhino, …)

– Hard to distribute code to other people

– Need to change files

• Where it could be used:– Medium to Large projects

Page 12: PerformanceTuning Ajax Applications

Dojo Packaging Overview

• What you will need:

– Ant, Rhino (Custom), …

• Profiles

– Determines the starting files to trace dependencies.

• Incorporate require and provides in your files.

Xap.provide("xap.application.Application");

Xap.require("xap.util.Exception");

http://www.dojotoolkit.com

Page 13: PerformanceTuning Ajax Applications

Concat Ant Task Overview

<concat destfile=“file_ab.js" force=“yes">

<filelist dir=“core“

files=“file_a.js"/>

<fileset dir=“extensions"

includes=“**/*.js“

excludes=“file_notneeded.js"/>

</concat>

• Problem – Not dependency based

• http://ant.apache.org/manual/CoreTasks/concat.html

Page 14: PerformanceTuning Ajax Applications

Reducing Size of Requests

• Techniques

– Remove white space and comments

– gZip

file_a.jsfile_a.js File_a_smaller.jsFile_a_smaller.jsProcessProcess

Page 15: PerformanceTuning Ajax Applications

Removing White Space

• Tools

– Open source – Packer, Dojo, …

– Share-ware - Safe Compress, ShrinkSafe…

• Careful of removing end of lines

– Semicolon is optional but needed if you remove the end of line

X = “Bob”

Y = 10

X = “Bob”

Y = 10X=“bob”y=10X=“bob”y=10ProcessProcess

Page 16: PerformanceTuning Ajax Applications

Removing White Space

• Tools

– Open source, Packer, Dojo, …

– Share-ware - Safe Compress, …

• Careful of removing end of lines

– Semicolon is optional but needed if you remove the end of line

X = “Bob”;

Y = 10;

X = “Bob”;

Y = 10;X=“bob”;y=10;X=“bob”;y=10;ProcessProcess

Page 17: PerformanceTuning Ajax Applications

Coding Style

• This can help make code much smaller

• Single line “if”, “for”… Do not need “{}”

• Combine var declarations into a single var

– var x=1,y=2;

• Use JS style object!

• …

Page 18: PerformanceTuning Ajax Applications

Gzip Compression

• Pluses

– Dramatically reduces size of file

330KB to 70KB

– Simple to do

Lots of gzip tools

• Minuses

– Lots of browser caveats

Page 19: PerformanceTuning Ajax Applications

Gzip Compression Caveats

• Content-Encoding must be gzip and Content-Type must be application/x-javascript; this requires web server configuration changes in most cases

• Browser must request HTTP 1.1 and must specify that it supports gzip

• Script tag must be between the HEADER tags in the HTML (solves IE bug with compressed JavaScript)

• Some client side and/or server code will be needed to detect the user agent and request the appropriate script file -- most web servers support this

Page 20: PerformanceTuning Ajax Applications

Other Items

• Minimize the code that is executed at startup

• Bring back data once screen is complete

• Loading images

– Lets users know something is going on

– Distracts the user from the time it is taking

Page 21: PerformanceTuning Ajax Applications

Runtime Performance

• High level

– Minimize the code

– use the native facilities

• Lower Level

– XML Parsing

– DOM Searching

– DOM Creation

– If statement optimization

Page 22: PerformanceTuning Ajax Applications

XML Parsing

• Don’t Write your own parser!

– Tried it in XAP: 10 – 100x slower than native parsers

• Use the Native Parser

– Encapsulated differences using Parser Factory

Does parsing

Normalizes DOM

• No native xPath

Page 23: PerformanceTuning Ajax Applications

Parser Differences

• Creating Parsers– IE: nativeDoc=new ActiveXObject("Microsoft.XMLDOM");

– Mozilla: parser=new DOMParser();

• Error Handling

– IE: check error code

– Mozilla: check returned document

Error document

• White space

Page 24: PerformanceTuning Ajax Applications

DOM Creation – innerHTML

• Pluses

– Fast

– Code can be small.document.getElementById(“myDiv”).innerHTML =

“<table><tr><td>New Table</td><tr></table>”;

• Minuses

– Code gets messy quickly when doing more complicate snippets of HTML.

Page 25: PerformanceTuning Ajax Applications

DOM Creation – innerHTML

• How YUI Does It

html[html.length] = "<table>";

html[html.length] = "<tr>";

html[html.length] = "<td>“;

html[html.length] = “My cell text”;

html[html.length] = "</td>“

domObject.innerHTML = html.join("\n");

Page 26: PerformanceTuning Ajax Applications

DOM Creation – Tail Recursion

• Create DOM Objectsvar table = document.createElement(“table”);

var tr = document.createElement(“tr”);

var td = document.createElement(“td”);

• Append in reverse ordertr.appendChild(td);

table.appendChild(tr);

onScreenElement.appendChild(table);

Page 27: PerformanceTuning Ajax Applications

Searching Dom Operations

• Use native

– getElementById()

– getElementsByTagName()

• Avoid complete DOM traversals

• toolkits

– Prototype

– jQuery

Page 28: PerformanceTuning Ajax Applications

Let’s Look at some low level JavaScript• Compare low level calls between browser

• Compare different way of doing things

– Getter/Setter vs direct property access

– For loop structure

– innerHTML vs appendChild

Page 29: PerformanceTuning Ajax Applications

Finding Bottlenecks

• By Handvar before = new Date();

//Do something that takes awhile

var after = new Date();

alert(after – before);

• Pluses

– Quick and easy

• Minuses

– You need to know where the problem is

– You need to change code

Page 30: PerformanceTuning Ajax Applications

jsLex Performance Tool

• Systematically Injects profile code

– Ant task

– Modified Mozilla Rhino engine

• Pluses

– Don’t need to change your code

– Metrics complete code base

– Don’t need to know where the bottleneck is

Page 31: PerformanceTuning Ajax Applications

jsLex Performance Tool - Continued• Pluses

– GUI console to look through the stats

– Save stats

– Compare stats

– Stack Traces

• Minuses – Will need to change the starting file

– Adding Metric causes probing effect

– Can slow code considerably

– Multi-step Workflow

Page 32: PerformanceTuning Ajax Applications

jsLex Workflow

1. Inject metric code into JavaScript files

– Creates function mapping file

2. Modify your HTML page to include jsLex

3. Run Page

4. Open mapping file

5. Click Snapshot

Page 33: PerformanceTuning Ajax Applications

Demo

Page 34: PerformanceTuning Ajax Applications

Questions?

Page 35: PerformanceTuning Ajax Applications

Thank You!

• For more information go to

– http://www.nexaweb.com

– http://incubator.apache.org/xap

• Home of jsLex

– http://www.rockstarapps.com

• Upcoming Webinar

– November 20th 2pm EST go to nexaweb.com to sign up.