Efficient JavaScript authoring techniques for finishing _javascript skills

Source: Internet
Author: User
Tags array length hasownproperty
Recently, a JavaScript framework has been written to show that there are many details that are not noticed enough and that long periods of accumulation can lead to serious efficiency problems when the framework is actually applied. So I began to focus on some of the techniques that JavaScript can do to improve efficiency, and share them here.

1.JavaScript is the only language that is as small as the size of the code required, so we can use some tools to streamline and compress JavaScript code, such as Jsmin, Packer, Yuicompressor, and so on. These tools replace the name of a local variable with a very short variable name, such as replacing parsefloat () with a (). So when we write JavaScript code, we should map each global variable to a local variable, such as var parsefloat = parsefloat;

2. By JSLint to detect the JavaScript you write, you can find many problems hidden inside. JSLint is a JavaScript verification tool (not open source) that scans JavaScript source code to find problems. If JSLint finds a problem, JSLint displays a message that describes the problem and points out the approximate location of the error in the source code.

3. When we write JavaScript, we often need to traverse an array with the following code:
Copy Code code as follows:

for (Var i=0;i<array.length;i++) {
Do something
}

The member variables of JavaScript are determined at runtime, which causes each loop to find the array length property, so we can add a variable to store the array size:
Copy Code code as follows:

var L = array.length;
for (Var i=0;i<l;i++) {
Do something
}

This seems to have been optimized, but in fact we can do better:
Copy Code code as follows:

var i=array.length;
while (i--) {
Do something
}

This is because these two lines of code are converted into assemblies, while the while statement requires less instruction, and there is no explanation here, and you are interested in studying the compilation.

4. Since anyone in JavaScript can modify or add attributes in Object.prototype, we should first use hasOwnProperty to judge the properties of an object, avoiding traversing the entire prototype chain and affecting efficiency. Such as:
Copy Code code as follows:

for (var key in obj) {
if (Obj.hasownproperty (key)) {
Do something
}
}

5. Define a local variable first when using undefined undefined
Copy Code code as follows:

var checkval = function (val) {
var undefined;
return Val!== undefined;
};

In the code above, if a local variable is not defined in advance, the global variable undefined is used directly to determine if a third party defines a global variable undefined=3 elsewhere, resulting in a result error.

6. When converting a variable of a non string type to a string type, you can use the
Copy Code code as follows:

var str = (i + ""). Replace (...);

This place is much slower if you use String (i).

7. When defining the array, if you do not need to use the array class and other methods, just for general assignment and access, you should write directly
Copy Code code as follows:

var array = {};

Instead of
Copy Code code as follows:

var array = new Array ();

Otherwise, it doesn't make sense to write var i = new Number (1) when defining a numeric variable.

8. When using jquery, perform multiple functions on the same object as much as possible in the same line of code, for example:
Copy Code code as follows:

$ ("P.neat"). AddClass ("Ohmy"). Show ("slow");

Instead of
Copy Code code as follows:

$ ("P.neat"). AddClass ("Ohmy");
$ ("P.neat"). Show ("slow");

The remaining tips include using DocumentFragment to optimize multiple append, using FirstChild and nextsibling instead of childnodes traversing DOM elements, and so on.
Specifically you can refer to this blog: http://www.nowamagic.net/librarys/veda/detail/363
This article also made reference to this article: http://www.cnblogs.com/justinw/archive/2009/12/07/1618500.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.