Performance improvement and analysis of JavaScript undefined

Source: Internet
Author: User

Performance improvement and analysis of Web page special effects undefined

At work we often need to determine whether a variable/attribute is undefined. There are usually two ways of writing
1//Mode 1
2 typeof age = = ' undefined ';
3
4//Mode 2
5 Age = = undefined

What is the difference between these two ways of writing? Which one should you use? Look at the following example
1 typeof age = = ' undefined '; True

The identifier age has not been declared, and the output is true.

Let's look at another example.
1 age = = undefined; Error


The following are not the correct methods:
var exp = undefined;
if (exp = = undefined)
{
Alert ("undefined");
}

When exp is null, it also gets the same result as undefined, although null and undefined are different. Note: This method can be used to determine both undefined and null.
var exp = undefined;
if (typeof exp = = undefined)
{
Alert ("undefined");
}

typeof returns a string of six possible types: number, String, Boolean, object, function, undefined.

The following are the correct methods:
var exp = undefined;
if (typeof exp = "undefined")
{
Alert ("undefined");
}

The following are the simpler and more correct methods (if the variable is neither Var nor assigned, the following error occurs, but the programmer, not JavaScript) is being reviewed:
var exp = undefined;
if (exp = = undefined)
{
Alert ("undefined");
}

Improve undefined performance

When we use the undefined value in a program, we actually use the undefined property of the Window object.

Similarly, when we define a variable but do not give its initial value, for example:

var avalue;

At this point, JavaScript sets its initial value to a reference to the Window.undefined property when called precompiled.

So when we compare a variable or value to a undefined, it is actually compared to the undefined property of the Window object. During this comparison, JavaScript searches for the properties of the window object called ' undefined ', and then compares the reference pointers of the two operands to the same.

Because the property values of the Window object are very numerous, it takes time to search for the undefined property of the Window object in each comparison with the undefined. This can be a performance problem in a function that requires frequent comparisons with undefined. Therefore, in this case, we can define a local undefined variable to speed up the comparison of undefined. For example:
function Anyfunc ()
{
var undefined; Custom Local undefined variables

if (x = = undefined)//reference comparison on scope


while (y!= undefined)//scope reference comparison

};

Where the undefined local variable is defined, its initial value is a reference to the Window.undefined property value. The newly defined local undefined variable exists on the scope of the function. In subsequent comparisons, the JavaScript code was written in a way that was not changed, but was relatively fast. Because the number of variables on the scope is much less than the properties of the Window object, the speed of the search variable can be greatly improved.

This is why many front-end JS frameworks often have to define a local undefined variable for themselves!

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.