Optimizing JavaScript code-1

Source: Internet
Author: User

There is an excellent article named
Optimizing JavaScript code. The authors are software engineers on Gmail
And Google Chrome.

I appreciate the knowledge described in this article and try to repeat it in my
Own Words.

Faster String concatenation

I have described this rule in my article
Javascript tips and tricks-5.

 
// Bad var longstr = "this is a very long" + "long" + "long string. "// good var longstr = [" this is a very long "," Long long "," long string. "]. join ("");

Notice: the original article useJoin ()To Concat string isIncorrect,
Which is the sameJoin (",").

Avoid create too extends temporary strings

Build up long strings by pass array into functions, to avoid create too using temporary
Strings.

Remeber in my last article-
Reference and array clone, I describe how an array is passing by reference.

 
// Bad function createmenu (INDEX) {return "<li> menu" + index + "</div>";} var arr = ["<ul>"]; for (VAR I = 0; I <100; I ++) {arr. push (createmenu (I);} arr. push ("</ul>"); var menu = arr. join (""); // good function createmenu (index, arr) {arr. push ("<li> menu" + index + "</div>");} var arr = ["<ul>"]; for (VAR I = 0; I <100; I ++) {createmenu (I, arr);} arr. push ("</ul>"); var menu = arr. join ("");

Define class method in prototype

// Bad function person (name) {This. name = Name; this. getname = function () {return this. name ;};}// good function person (name) {This. name = Name;} person. prototype. getname = function () {return this. name ;};

The first method is inefficient, as each time a instance of person is constructed,
A new function and closure is created.

I also mention the prototype property in this article-
Prototype/constructor that I have known

Don't define reference type in prototype

I have describe this rule in my article-
Athena JavaScript questions-2.

You can also refer to that article to discover why we shoshould not define reference
Type in prototype.

But this Google article suggest define value type variables in prototype (such
Number, Boolean, String, null, undefined ).

The purpose is to avoid unnecessarily running the initialization code each time
The constructor is called.

For example:

 
// Google prefered way: function person (name) {If (name) {This. name = Name ;}} person. prototype. name = "unknown"; person. prototype. getname = function () {return this. name ;}; var p1 = new person (); console. log (p1.getname (); // "unknown" Var P2 = new person ("zhangsan"); console. log (p2.getname (); // "zhangsan"

Notice: We don't have to initialize the name property when the P1 is under construction.

This may speed up the initialization and save some memory storage.

But the effects isVery littleIf the variable is not a very long string.

Therefore, I perfer writing the previous example in this way:

 
// I prefered way: function person (name) {This. name = Name | "unknown";} person. prototype. getname = function () {return this. name ;}; var p1 = new person (); console. log (p1.getname (); var P2 = new person ("zhangsan"); console. log (p2.getname ());

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.