201506150846_ "JavaScript authoritative Guide (sixth Edition)-performance optimization 10, lowercase to uppercase, filtering, Functions" (p162-168)

Source: Internet
Author: User

I. Authoritative guidelines

1. For class array objects, we cannot use the array method, but we can use Function.call ();

For example: Array.prototype.slice.call (arr,...);

Array.prototype.silice.call (arr,...);

Array.prototype.map.call (arr,...);

Two. Performance Optimization 10

1. var Someid = document.getElementById (' #someElem '); Change to: var docu = document, Someid = Docu.getelementbyid (' #someElem ');

2. Do not use with ();

3. Use closures carefully, especially in the loop;

4. In the data, variables, object properties and array elements of 4 access methods, the speed of the former is significantly better than the latter two;

5. Do not dig too deep in the array;

6. Use for (), or do-while to replace for-in;

7. In the loop, the loop condition and the loop variable are merged together;

8. Define an array for the HTML collection object;(recommended for high-performance JavaScript authors)

For example: function array (items) {

try {


Aarray.prototype.concat.call (items);

}catch (ex) {

var i = 0,

Len = Items.length,

result = Array (len);

while (I < Len) {

Result[i] = items[i];



return result;



var docu = document;

var divs = Array (docu.getelementbytagname (' div '));

for (var i =0; i < divs.length;i++) {

var mydiv = docu.createlement ("div");

Docu.appendchild ("mydiv");


9. Don't touch the DOM!  

Crateattribute (name);

Createcomment (text);

Createdocumentfragment (); (recommended usage)

CreateElement (tagname);

createTextNode (text);

10. Modify CSS classes rather than styles

Three. Lowercase to uppercase

For example:

var str = [' A ', ' P ', ' P ', ' l ', ' e ', ' s '];

Array.map = Array.map | | function (A,SEP) {return Array.prototype.map.call (A,SEP)};
var newstr = Array.map(str,function (elem) {return elem.touppercase ()});
Console.log (NEWSTR);

Four. Filtering

For example:

var s = "Fsdgnxvhdzfgtrxzcsdightmjtioyujvdgs";
var NewS = Array.prototype.join.call (S, "");

Console.log (NewS); //f s d G N x v h d z F g t R x Z c s d i g h t M J t i o y u J v d G S

var lastests = Array.prototype.filter.call (news,function (elem) {return elem.match (/[^aeiou]/);}). Join ("");

Console.log (lastests); //fsdgnxvhdzfgtrxzcsdghtmjtyjvdgs

Note: The string is immutable, non-modifiable (more special), using the push (), sort (), reverse () method to modify the array, but invalid for the string!!

Five. Functions

1. A function that initializes an object becomes a constructor (constructor).

2. There are two ways of defining a function: A. Define statement b. expression

3. Example 1: Traversing object property names and properties

var object = {' A ': ' 3 ', ' str ': ' Hello ', ' funcion ': ' fn ', ' B ': ' 0.618 '};

function Printprops (obj) {for (var p in obj) Console.log (p + ":" + obj[p] + "\ n")};

Printprops (object); //





Example 2: Calculating the distance of two coordinates

function Distance (x1,x2,y1,y2) {

var dx = x2-x1;

var dy = y2-y1;

Return math.sqrt (DX*DX + dy*dy);


Distance (0,0,2.8,55); //0?

Example 3: Factorial

function factorial (n) {

if (n <= 1) {

return 1;



N*factorial (n-1)



Factorial (4); //24

4. The name of the function will be a local variable inside the function;

5. Functions that do not return values sometimes become "processes";

6. Note:x can access a,b!!

For example:

function Fn_a (A,b) {

function Fn_b (x) {

return x*x;


Return Math.sqrt (Fn_b (a) + Fn_b (b));


7. Function expressions can appear anywhere in the JavaScript code

Six. Function calls

1. As a function call, as a method call, as a constructor invocation, call () and apply () indirect call;


201506150846_ "JavaScript authoritative Guide (sixth Edition)-performance optimization 10, lowercase to uppercase, filtering, Functions" (p162-168)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.