! + "\ V1 ″&! + [1,] &!" 1 "[0] faster than anyone else

Source: Internet
Author: User

What we have done below may be a bit of a taste of jumping bones in the eggs, but it is also very interesting. I hope you will be able to gain something after reading it.

In general, browser compatibility issues have plagued developers. hardworking developers have also come up with a variety of tips to differentiate browsers of different factions.

! + "\ V1"
This is what we saw in situ zhengmei's shoes in the garden. At first glance, we were shocked. It turned out that we could be so cool.
The following is my understanding. Source: link 1.
This is a javascript type conversion technique. According to the priority, here is from right to left (+ get positive>! Calculate the negative logic ). that is, convert the string "\ V1" to a number and then reverse it. it is easy to understand the inverse of the number. As long as the values not 0 are true, the + operator converts the string to a number. then, ie is ambiguous here. The Escape Character "\ v" and vertical tab are incorrectly understood. There are more explanations about the Tab character link 2 at the end of the text. the tab can be correctly interpreted in other browsers. ie interprets the tab as the letter "V", which of course cannot be converted to numbers. Naturally, Nan is converted according to the conversion rules of logical operators, if it is converted to false, another reverse request is converted to true, which means "I am IE, yes, true! ".
 

! + [1,]
We also saw it in situ children's shoes, and there were a lot of items to receive from the garden every day. For the explanation of this judgment statement, please move to Situ zhengmei-the shortest ie judgment in the whole world.
However, in the next test, you will find that the minimum statement is not necessarily the fastest.

 

! "1" [0]
This was found when I looked at the ext source code. In the comments of Ext. toarray, the author told IE that the array subscript cannot be used to access the string. Can this be distinguished.
This method is used. In ie, the string cannot be accessed as an array subscript. In ext annotations, the system prompts that "ABC" can be used ". match (/. /g) method, I wondered how to directly use the native split method, "ABC ". split (""), obviously more intuitive, and removes the need for a regular expression.
This method is more efficient than IE in ff.
Note

 

IE8
Judgment statement Three tests Average Value
! + "\ V1" 3776 3682 3681 3713
! + [1,] 58422 56519 56581 57174
! "1" [0] 5445 5554 5507 5502
! Window. addeventlistener 23045 22825 22908 22926
Window. attachevent 29419 29450 29556 29475
Document. All 44553 44647 44835 44678
 
Firefox3.6
! + "\ V1" 1425 1428 1425 1426
! + [1,] 12627 12478 12560 12555
! "1" [0] 125 140 126 130
! Window. addeventlistener 27236 25530 25556 26107
Window. attachevent 77590 79494 77347 78143
Document. All 22877 22984 22978 22946

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Test Environment CPU: e2180 (2.0 GHz), Ram 2G. OS: win7

 

Test code

VaR I = 0, total = 10000000, btimespan, p1timespan, p2timespan;
VaR bstart = new date ();
For (; I <total; I ++ ){
If (/* test statement */);
}
Btimespan = new date (). gettime ()-bstart. gettime ();

I = 0;
VaR p1start = new date ();
For (; I <total; I ++ ){
If (/* test statement */);
}
P1timespan = new date (). gettime ()-p1start. gettime ();

Connection 1: 32 bytes, ehr... 9, ehr... 7 !!! To know if your browser is ie -- http://webreflection.blogspot.com/2009/01/32-bytes-to-know-if-your-browser-is-ie.html (Wall flip required)
Link 2: Escape Character _ Baidu encyclopedia -- http://baike.baidu.com/view/73.html
Connection 3: javascript conversions-http://www.cnblogs.com/dreampuf/archive/2009/01/22/1380075.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.