Home > Article > Web Front-end > Summary of methods to optimize JavaScript code_javascript skills
Optimizing JavaScript Code
Author: Gregory Baker, GMail Software Engineer and Erik Arvidsson, Google Chrome Software Engineer
Required experience: Working knowledge of JavaScript
Client-side scripting can make your application more dynamic and active, but browser parsing of the code may cause efficiency issues, and this performance difference varies between clients. Here we discuss and give some tips and best practices for optimizing your JavaScript code.
Using Strings
The string concatenation operation will have a great impact on the garbage collection of Internet Explorer 6 and 7. Although this problem is solved in Internet Explorer 8 - String Concatenation Slightly more efficient in IE8 and other non-IE browsers (such as Chrome) - if a large proportion of your users are using Internet Explorer 6 or 7, you need to be very careful about how you build strings.
There is the following sample code:
Check out IE JScript Performance Recommendations Part 3: JavaScript Code inefficiencies for more information on using closures in IE.
<SPAN style="COLOR: #336600">with</SPAN>
Avoid using in your code<SPAN style="COLOR: #336600">with</SPAN>
. It has a very bad impact on performance because it modifies the scope chain, making it expensive to look up variables in other scopes.
Memory leaks are a very common problem for web applications, and they can cause serious performance problems. When the browser's memory usage increases, your web application, along with the rest of the user's system, will slow down. The most common causes of memory leaks in web applications are: circular references between the JavaScript engine and the browser DOM's C object implementation (for example, between the JavaScript engine and Internet Explorer's COM infrastructure, or between the JavaScript engine and Firefox's XPCOM infrastructure Architectural room).
Here are some rules of thumb to avoid memory leaks:
The most common circular reference pattern [DOM element--> event handler--> closure scope--> DOM] is in this article MSDN Discussed in the blog article. To avoid this problem, you can use a well-tested event system to attach event handlers, such as Google doctype, Dojo , or JQuery.
In addition, using inline event handlers in IE will cause another type of leak. This is not a usual circular reference leak, but a leak of temporary anonymous script objects in memory. Please see for details Understanding and Solving Internet Explorer Leak Patterns 's "DOM Insertion Order Leak Model" section, also in There is also an example in the JavaScript Kit tutorial.
Extended attributes are arbitrary JavaScript attributes attached to DOM elements and are a common cause of circular references. You can use extended attributes without causing a memory leak, but it is easy to accidentally introduce a leak. The pattern of this leak is [DOM Element--> Extended Attributes--> Intermediate Object--> DOM Element]. The best way is to avoid using them. If you are going to use them, just use simple value types. If you are going to use Simple type, then set the extended attribute to null when it is no longer needed. See Understanding and Solving Internet Explorer Leak Patterns "Circular References" section.