General Interface is an open source project hosted by the Dojo Foundation

JavaScript Code

Use the following best practices when developing JavaScript code for your application.

Remove Unused JavaScript Code

Remove unused portions of code to make the JavaScript file smaller, speed up the JavaScript loading time, and make the code run faster.

Avoid Initializing Unused Variables

Try to avoid initializing variables that might not be used. Initialize them after checking to see if they are needed. Logging messages, which are only needed during development, are a good example of unused variables.

Replace Old and Deprecated APIs with More Efficient APIs

Replace old and deprecated APIs with new, more efficient APIs. For example, use the faster jsx3.xml.Entity.getChildIterator(),getAttributeNames(), and selectNodeIterator() methods instead of getChildNodes(), getAttributes(), and selectNodes() respectively.

Another example is to use jsx3.util.List instead of jsx3.util.Collection. Change the following use of Collection:

for (var i=0;i<collection.getLength();i++){
   var x = collection.getItem(\i);
}

to List:

for (var it = objList.iterator();it.hasNext();\){
   var x = it.next();
}

For more information, see General Interface API Reference.

Replace JavaScript Manipulation of CDF with XSL Transformations

Replace JavaScript manipulation of CDF with faster, more efficient XSL transformations. For example, if fifty customer records are returned from a jsx3.net.Request response, iterating through all the elements in JavaScript is less efficient than building an XSL template and applying it using the jsx3.xml.Template API.

GUI components that implement jsx3.xml.Cacheable have built-in properties for XSL transformations. See the XML Transformers property in the Properties Editor palette and jsx3.xml.Cacheable.setXMLTransformers() in General Interface API Reference.

Use jsx3.net.Service.compile()

Use the jsx3.net.Service.compile() method to compile the CXF rules for a service instance to an equivalent XSLT document. This enables much faster performance than using the DOM-based iterator (default) to convert the XML response document into a CDF Document type. Call this method immediately before or after the doCall() method for best performance.

For more information, see jsx3.net.Service.compile() in General Interface API Reference.

Avoid Excessive String Concatenations

In Internet Explorer, string concatenations are very expensive, especially with large strings. A better approach is to replace string concatenations with an array, push new strings to the array, and then use an Array.join() method call to get a single string. This approach essentially uses a string buffer instead of successive concatenations. For more information, see window.Array.join() in General Interface API Reference.

Identify Slow Functions Calls

When possible, identify and optimize function calls that are taking excessive time. Use Firebug for Firefox and jsLex for Internet Explorer to profile your JavaScript code and identify slow functions.

In Internet Explorer 6, beware of String and RegExp literals in loops. For best performance, declare these literals statically or declare them outside of loops.

Contents

Searching General Interface Docs

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.