spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / optimize / speedup / chap10 / 1 To page 1current pageTo page 3To page 4To page 5
[previous] [next]

Speed Up Your Site, Chapter 10: Optimizing JavaScript for Execution Speed

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


Algorithms and Data Structures

As we learn in computer science classes, global optimizations (such as algorithm and data structure choices) determine in large part the overall performance of our programs. For larger values of "n," or the number of input elements, the complexity of running time can dominate any local optimization concerns. This complexity is expressed in O-notation, where complexity or "order" is expressed as a function of n. Table 10.1 shows some examples.

Table 10.1 Run-Time Complexity of Classic Algorithms[12],[13]

Notation

Name

Example

O(1)

constant

array index, simple statements

O(logn)

logarithmic

binary search

O(n)

linear

string comparison, sequential search

O(nlogn)

nlogn

quicksort and heapsort

O(n2)

quadratic

simple selection and insertion sorting methods (two loops)

O(n3)

cubic

matrix multiplication of nxn matrices

O(2n)

exponential

set partitioning (traveling salesman)

Array access or simple statements are constant-time operations, or O(1). Well-crafted quicksorts run in nlogn time or O(nlogn). Two nested for loops take on the order of nxn or O(n2) time. For low values of n, choose simple data structures and algorithms. As your data grows, use lower-order algorithms and data structures that will scale for larger inputs.

Use built-in functions whenever possible (like the Math object), because these are generally faster than custom replacements. For critical inner loops, measure your changes because performance can vary among different browsers.

Refactor to Simplify Code

Refactoring is the art of reworking your code to a more simplified or efficient form in a disciplined way. Refactoring is an iterative process:

  1. Write correct, well-commented code that works.

  2. Get it debugged.

  3. Streamline and refine by refactoring the code to replace complex sections with shorter, more efficient code.

  4. Mix well, and repeat.

Refactoring clarifies, refines, and in many cases speeds up your code. Here's a simple example that replaces an assignment with an initialization. So instead of this:

function foo() {
  var i;
  // ....
  i = 5;
}

Do this:

function foo() {
  var i = 5;
  // ....
}

For More Information - Refactoring is a discipline unto itself. In fact, entire books have been written on the subject. See Martin Fowler's book, Refactoring: Improving the Design of Existing Code (Addison-Wesley, 1999). See also his catalog of refactorings at http://www.refactoring.com/.



home / programming / optimize / speedup / chap10 / 1 To page 1current pageTo page 3To page 4To page 5
[previous] [next]

12. Kernighan and Pike, The Practice of Programming, 41. Back

13. Andrew Hunt and David Thomas, The Pragmatic Programmer: From Journeyman to Master (Boston, MA: Addison-Wesley, 1999), 179. Back

From Speed Up Your Site: Web Site Optimization, Chapter 10.
Copyright 2003. Reproduced by permission of New Riders.

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: January 8, 2003
Revised: January 8, 2003

URL: http://webreference.com/programming/optimize/speedup/chap10/1/2.html