Efficient JavaScript compiling skills _ javascript skills

Source: Internet
Author: User
Tags hasownproperty
I recently wrote a JavaScript framework and found that many of the details are not enough. So I began to focus on some tips for JavaScript to improve efficiency. I would like to share with you here, if you are interested, you can refer to the recently written JavaScript framework and find that many details are not enough to pay attention to. You are worried that a long accumulation may cause serious efficiency problems in the actual application of the Framework. So I began to focus on some tips for JavaScript to improve efficiency. I would like to share with you here.

1. JavaScript is the only language that requires a smaller size of code. Therefore, we can use some tools to streamline and compress JavaScript code, such as JSMin, Packer, and YUICompressor. These tools will replace the local variable name with a very short variable name, for example, replacing parseFloat () with (). Therefore, when writing JavaScript code, we should map every global variable to a local variable, such as var parseFloat = parseFloat;

2. Use JSLint to detect your JavaScript code and discover many hidden problems. JSLint is a JavaScript verification tool (non-open source) that can scan JavaScript source code to find problems. If JSLint finds a problem, JSLint displays a message describing the problem and points out the approximate location of the error in the source code.

3. When writing JavaScript, we often need to traverse an array. The Code is as follows:

The Code is as follows:


For (var I = 0; I // do something
}


The JavaScript member variables are determined at runtime, which leads to the need to search for the array length attribute for each loop. Therefore, we can add a variable to store the array size:

The Code is as follows:


Var l = array. length;
For (var I = 0; I // Do something
}


This seems to have been well optimized, but we can actually do better:

The Code is as follows:


Var I = array. length;
While (I --){
// Do something
}


This is because when the two lines of code are converted into an assembly, the while statement requires less instructions, which will not be described here. If you are interested, you can study the assembly.

4. because anyone in JavaScript can modify or add objects. attribute in prototype. Therefore, when traversing an object's attribute, we should first use hasOwnProperty for judgment to avoid traversing the entire prototype chain and affecting efficiency. For example:

The Code is as follows:


For (var key in obj ){
If (obj. hasOwnProperty (key )){
// Do something
}
}


5. When undefined is used, define a local variable undefined first.

The Code is as follows:


Var checkVal = function (val ){
Var undefined;
Return val! = Undefined;
};


In the above Code, if a local variable is not defined in advance, the global variable undefined is used for determination. If a third party defines a global variable undefined = 3 elsewhere, the result is incorrect.

6. You can directly use

The Code is as follows:


Var str = (I + ""). replace (...);


In this case, String (I) is much slower.

7. When defining arrays, if you do not need to use methods such as sorting array classes, just assign values and access them, you should directly write

The Code is as follows:


Var array = {};


Instead

The Code is as follows:


Var array = new Array ();


Otherwise, it makes no sense to write var I = new Number (1) when defining a numeric variable.

8. When using jQuery, execute multiple functions for the same object in the same line of code as much as possible. For example:

The Code is as follows:


$ ("P. neat"). addClass ("ohmy"). show ("slow ");


Instead

The Code is as follows:


$ ("P. neat"). addClass ("ohmy ");
$ ("P. neat"). show ("slow ");


Other tips include using DocumentFragment to optimize multiple appends, and using firstChild and nextSibling instead of childNodes to traverse dom elements.
You can refer to this blog: http://www.nowamagic.net/librarys/veda/detail/363
This article also references this article: http://www.cnblogs.com/justinw/archive/2009/12/07/1618500.html

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.