May 06, 2021 JavaScript
1. Do not use with() statements
2. Object properties and array elements are slower than variables
5. The number is converted into a string
6. Replace CreativeElement with the template element clone
7. Avoid inefficient script locations
9. The control conditions and control variables are combined during the loop
10.. Use XMLHttpRequest (XHR) objects
12.. Avoid comparisons with null
13.. Respect the ownership of the object
16.. Various types of conversions
17.. Multiple type declarations
20.. Avoid double interpretation
21.. Shorten the negative detection
22.. Release the javascript object
23.. Performance considerations
24.. Avoid errors where you should be aware of them
25.. The difference between a s and a s
27.. The function returns a uniform type
28.. Always check the data type
29.. When to use single quotes and when to use double quotes
JavaScript already has an important place in many languages, and there are many things we can do with JavaScript that can be used in a wide range of applications. I
n web applications, a lot of JavaScript code is required, and more and more will be necessary in the future. H
owever, because JavaScript is a language for interpreting execution, and its single-threaded mechanism determines that performance issues are a weakness of JavaScript and a problem that developers need to be aware of when writing JavaScript, web 2.0 applications often experience poor performance, mainly due to insufficient JavaScript performance, resulting in overburdened browsers. J
avaScript performance optimization is by no means a written skill, so how should JavaScript code be loaded and executed correctly to improve its performance in the browser?
Here's a summary of your knowledge of optimization tips.
Whether the current JavaScript code is embedded or in an external chain file, the download and rendering of the page must stop and wait for the script to execute. T he longer javaScript takes to execute, the longer the browser waits for user input. B rowsers are blocked when downloading and executing scripts because the scripts can change the page or JavaScript's namespace, which can affect the content of later pages. A typical example is when using:
document.write()
Example:
<html>
<head>
<title>Source Example</title>
</head>
<body>
<p>
<script type="text/javascript">
document.write("Today is " + (new Date()).toDateString());
</script>
</p>
</body>
</html>
When the browser encounters a tag, the current HTML page has no way of knowing whether JavaScript will add content to the tag, introduce other elements, or even remove the label. T
herefore, the browser stops working on the page, executes JavaScript code, and then continues to parse and render the page. T
he same thing happens when you load JavaScript using the src property, and the browser must first take the time to download the code in the chain file and then parse and execute it.
During this process, page rendering and user interaction are completely blocked.
So when you reference an object property or array element multiple times, you can get a performance boost by defining a variable. ( This rule works when reading and writing data) Although this rule is correct in most cases, Firefox has done some interesting work in optimizing array indexes to make them perform better than variables. However, given the performance disadvantages of array elements on other browsers, you should try to avoid array lookups unless you are really only developing for the performance of Firefox.
function search() {
//当我要使用当前页面地址和主机域名
alert(window.location.href + window.location.host);
}
//最好的方式是如下这样 先用一个简单变量保存起来
function search() {
var location = window.location;
alert(location.href + location.host);
}
with (a.b.c.d) {
property1 = 1;
property2 = 2;
}
//可以替换为:
var obj = a.b.c.d;
obj.property1 = 1;
obj.property2 = 2;
(“” +) > String() > .toString() > new String()
var frag = document.createDocumentFragment();
for (var i = 0; i < 1000; i++) {
var el = document.createElement('p');
el.innerHTML = i;
frag.appendChild(el);
}
document.body.appendChild(frag);
//替换为:
var frag = document.createDocumentFragment();
var pEl = document.getElementsByTagName('p')[0];
for (var i = 0; i < 1000; i++) {
var el = pEl.cloneNode(false);
el.innerHTML = i;
frag.appendChild(el);
}
document.body.appendChild(frag);
<html>
<head>
<title>Source Example</title>
<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="script3.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<p>Hello world!</p>
</body>
</html>
However, this routine hides serious performance issues. I
n the example in Listing 2, when the browser resolves to the label (line 4), the browser stops parsing the contents thereafter, giving priority to downloading the script file and executing the code in it, which means that neither the styles.css style file nor the label can be loaded, and the page will naturally not render because the label cannot be loaded. S
o the page is blank until the JavaScript code is fully executed.
The following image describes the download process for scripts and style files during page loading.
<head>
<title>Source Example</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<p>Hello world!</p>
<!-- Example of efficient script positioning -->
<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="script3.js"></script>
</body>
</html>
This code shows the recommended location for placing the hashtag in the HTML document. A
lthough the script download blocks another script, most of the content of the page has been downloaded and displayed to the user, so the page download does not appear too slow.
This is the first rule for optimizing JavaScript: put the script at the bottom.
document.getElementById('foo').onclick = function(ev) { };
The problem with closures is that, by definition, there are at least three objects in their scope chain: closure variables, local variables, and global variables. T
hese additional objects will cause other performance issues.
But Nicholas doesn't want us to waste food, and closures are useful for improving code readability, just don't abuse them (especially in loops).
Tip: If you don't know much about JavaScript closures, please refer to the JavaScript closure section of this site!
When it comes to performance, work that needs to be avoided in a loop has always been a hot topic because the loop is repeated many times. So if there is a need for performance optimization, the most obvious performance improvement is likely to be achieved by opening the loop first.
One way to optimize a loop is to combine control conditions and control variables when defining a loop, and here's an example of not merging them:
for ( var x = 0; x < 10; x++ ) {
};
Before we add something to the loop, we find that several operations occur in each iteration.
The JavaScript engine requires:
#1:检查 x 是否存在
#2:检查 x 是否小于 0 <span style="color: #888888;">(这里可能有笔误)</span>
#3:使 x 增加 1
However, if you are only an iterative element, you can use the while loop for rotation instead of the above:
var x = 9;
do { } while( x-- );
<script>
the page with a dynamic element.
var xhr = new XMLHttpRequest();
xhr.open("get", "script1.js", true);
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
var script = document.createElement ("script");
script.type = "text/javascript";
script.text = xhr.responseText;
document.body.appendChild(script);
}
}
};
xhr.send(null);
This code sends a GET request to the server .js script1 file. T
he onreadystatechange event handler checks readyState for 4 and then checks that the HTTP status code is valid (2XX for a valid response and 304 for a cache response). I
f a valid response is received, create a new element, setting its text properties to the responseText string received from the server. D
oing so actually creates an element with inline code.
Once the new element is added to the document, the code is executed and ready to be used.
Tip: You can also learn how to create an XHR object in the XHR Creation Objects section of the AJAX Tutorial on this site!
var images = document.getElementsByTagName('img');
for (var i = 0, len = images.length; i < len; i++) {
}
When writing JavaScript, be sure to know when to return NodeList objects so that access to them is minimized
Because JavaScript is a weak type, it doesn't do any automatic type checking, so if you see code compared to null, try replacing it with the following techniques:
1, if the value should be a reference type, use the instanceof operator to check its constructor
2, if the value should be a basic type, the role of typeof to check its type
3. If you want the object to contain a specific method name, use the typeof operator to ensure that the method that specifies the name exists on the object
var el = document.getElementById('MyElement');
var func = function () {
//…
}
el.func = func;
func.element = el;
This is not usually the case.
Typically, circular references occur when a closure is added to a dom element as an expendo.
function init() {
var el = document.getElementById('MyElement');
el.onclick = function () {
//……
}
}
init();
When init is executing, the current context is called context. A t this point, context refers to el, el refers to fusion, function refers to context. This is when a circular reference is formed.
There are two ways to resolve circular references:
function init() {
var el = document.getElementById('MyElement');
el.onclick = function () {
//……
}
}
init();
//可以替换为:
function init() {
var el = document.getElementById('MyElement');
el.onclick = function () {
//……
}
el = null;
}
init();
Empty el, context does not contain a reference to the dom object, interrupting the looping application.
If we need to return the dom object, we can do so in the following way:
function init() {
var el = document.getElementById('MyElement');
el.onclick = function () {
//……
}
return el;
}
init();
//可以替换为:
function init() {
var el = document.getElementById('MyElement');
el.onclick = function () {
//……
}
try {
return el;
} finally {
el = null;
}
}
init();
function init() {
var el = document.getElementById('MyElement');
el.onclick = function () {
//……
}
}
init();
//可以替换为:
function elClickHandler() {
//……
}
function init() {
var el = document.getElementById('MyElement');
el.onclick = elClickHandler;
}
init();
Draw the function into the new context so that the function's context does not contain a reference to el, thus interrupting the circular reference.
Under IE, the script creates the dom object, if there is no append to the page, refresh the page, this part of the memory will not be reclaimed!
function create() {
var gc = document.getElementById('GC');
for (var i = 0; i < 5000; i++) {
var el = document.createElement('div');
el.innerHTML = "test";
//下面这句可以注释掉,看看浏览器在任务管理器中,点击按钮然后刷新后的内存变化
gc.appendChild(el);
}
}
If you want to connect more than one string, you should use less, such as
s+=a;
s+=b;
s+=c;
Should be written as s
And if you're collecting strings, such as multiple times working on the same string, it's a good idea to use a cache, use a JavaScript array to collect them, and finally connect them using the jon method
var buf = [];
for (var i = 0; i < 100; i++) {
buf.push(i.toString());
}
var all = buf.join("");
JavaScript programming live: How JavaScript strings are connected
var myVar = "3.14159",
str = "" + myVar, // to string
i_int = ~ ~myVar, // to integer
f_float = 1 * myVar, // to float
b_bool = !!myVar, /* to boolean - any string with length
and any number except 0 are true */
array = [myVar]; // to array
If the toString() method is defined
for type conversion, it is recommended to explicitly call
to toString(), because
internal operations will try to convert
the object's toString()
method after all possibilities have been tried, so it would be more efficient to call this method directly
var aTest = new Array(); //替换为 var aTest = []; var aTest = new Object; //替换为 var aTest = {}; var reg = new RegExp(); //替换为 var reg = /../; //如果要创建具有一些特性的一般对象,也可以使用字面量,如下: var oFruit = new O; oFruit.color = "red"; oFruit.name = "apple"; //前面的代码可用对象字面量来改写成这样: var oFruit = { color: "red", name: "apple" };
var num = 0;
setTimeout('num++', 10);
//可以替换为:
var num = 0;
function addNum() {
num++;
}
setTimeout(addNum, 10);
if (oTest != '#ff0000') {
//do something
}
if (oTest != null) {
//do something
}
if (oTest != false) {
//do something
}
//虽然这些都正确,但用逻辑非操作符来操作也有同样的效果:
if (!oTest) {
//do something
}
In rich applications, memory consumption increases as the number of instantiated objects increases. Therefore, references to objects should be released in a timely manner so that the GC can reclaim these memory controls.
Object: obj s null
Object properties: delete obj.myproperty
Array item: Use the array's splice method to release the items that are not used in the array
1, try to use the native method
2, switch statement is relatively fast
Organize case statements in the most likely to least likely order
3, bit operation is faster
Bit operations are faster than any Boolean or arithmetic operations when numeric operations are performed
4, clever use of || and the operator of the and the operator of the
function eventHandler(e) {
if (!e) e = window.event;
}
//可以替换为:
function eventHandler(e) {
e = e || window.event;
}
if (myobj) {
doSomething(myobj);
}
//可以替换为:
myobj && doSomething(myobj);
1, the end of each statement must be added a sign
In an if statement, even if the conditional expression has only one statement, it is enclosed with a statement, lest a subsequent logical error be caused if the statement is added
2. Be careful when using the plus sign
Unlike other programming languages, in JavaScript, in addition to representing the addition of numeric values and the connection of strings, a string can be used as a meta-operator to convert strings to numbers. Therefore, if used improperly, it may be confused with the self-adder ' and cause a computational error
var valueA = 20;
var valueB = "10";
alert(valueA + valueB); //ouput: 2010
alert(valueA + (+valueB)); //output: 30
alert(valueA + +valueB); //output:30
alert(valueA ++ valueB); //Compile error
3, the use of return statements need to pay attention to
A return statement with a return value should not enclose the return value with () parentheses, and if an expression is returned, the expression should be on the same line as the return keyword to avoid compression when the compression tool automatically adds a semically sign, resulting in a return that is inconsistent with the developer
function F1() {
var valueA = 1;
var valueB = 2;
return valueA + valueB;
}
function F2() {
var valueA = 1;
var valueB = 2;
return
valueA + valueB;
}
alert(F1()); //output: 3
alert(F2()); //ouput: undefined
var valueA = "1";
var valueB = 1;
if (valueA == valueB) {
alert("Equal");
}
else {
alert("Not equal");
}
//output: "Equal"
if (valueA === valueB) {
alert("Equal");
}
else {
alert("Not equal");
}
//output: "Not equal"