Prototype source code analysis of String (1)-related indexOf optimizer _ prototype

Source: Internet
Author: User
Prototype source code analysis String section (1) about indexOf optimization. For more information, see. There are many methods added to String. prototype, but they are roughly divided into the following categories:

Category Method Name
Original capability enhancement Strip|Include|StartsWith|EndsWith|Empty|Blank
Format Camelize | capitalize|Underscore|Dasherize|Inspect
Deformation ToArray|Succ|Times
Replace Interpolate|Sub|Scan|Truncate | gsub
HTML processing StripTags|EscapeHTML|UnescapeHTML
Parameter serialization ToQueryParams
JSON Processing UnfilterJSON|IsJSON|EvalJSON|ParseJSON
Script Processing StripScripts|ExtractScripts|EvalScripts

Starting from the basic enhancement of original capabilities, the following is a specific implementation, which is well understood:

The Code is as follows:


(Function (s ){
Function strip (){
Return this. replace (/^ \ s +/, ''). replace (/\ s + $ /,'');
}
Function include (pattern ){
Return this. indexOf (pattern)>-1; // split
}
Function startsWith (pattern ){
Return this. lastIndexOf (pattern, 0) = 0;
}
Function endsWith (pattern ){
Var d = this. length-pattern. length;
Return d> = 0 & this. indexOf (pattern, d) = d;
}
Function empty (){
Return this = '';
}
Function blank (){
Return/^ \ s * $/. test (this );
}
S. strip = String. prototype. trim | strip;
S. include = include;
S. startsWith = startsWith;
S. endsWith = endsWith;
S. empty = empty;
S. blank = blank;
}) (String. prototype );


The strip above is $. trim in jquery, and most of them seem to be trim. The tragedy of directly expanding the native prototype is shown here, because the trim method is implemented in the subsequent JS implementation (such as chrome), and it is self-defeating.

The Code is as follows:


Function strip (){
Return this. replace (/^ \ s +/, ''). replace (/\ s + $ /,'');
}


Here replace (/^ \ s +/, '') is trimLeft, replace (/\ s + $/,'') is trimRight, but Prototype. the two methods are not available in String.

Below are some interesting points:

At that time, I was puzzled by startsWith and endsWith. It is reasonable to say that startsWith can use indexOf, but lastIndexOf is used here. Later I flipped through the implementation of Prototype1.6:

The Code is as follows:


Function startsWith (pattern ){
Return this. indexOf (pattern) = 0;
}

Function endsWith (pattern ){
Var d = this. length-pattern. length;
Return d> = 0 & this. lastIndexOf (pattern) = d;
}


It can be seen that in earlier versions, startsWith used indexOf, but in version 1.7 it modified the implementation of startsWith. In MySQL 1.7:

In startsWith implementation, lastIndexOf looks forward from the back, but the start point (fromindex) is set to 0. Therefore, you only need to check the start time.
In the endsWith implementation, indexOf searches from front to back. Because the string length is not fixed, the length is calculated here, and then the start point (fromindex) is determined. Therefore, you only need to check the last time.

The performance optimization here is that, in the implementation of 1.6, if there is no matching at the beginning (that is, startsWith is not true), but indexOf will still look back until a matching or string is found, this is a waste. For example, for the following operation:

'Abcdefgabcdefg'. startsWith ('abc ')
There is no difference between the implementation of version 1.6 and version 1.7, but let's switch to the following:

'Abcdefgabcdefg'. startsWith ('xesam ')
In the 1.6 implementation, the indexOf operation in startsWith will continue searching backward even if a does not match x at the beginning, until the matching 'xesam' or the end of the string is found.
In the 1.7 implementation, lastIndexOf in startsWith is reverse lookup (fromIndex = 0). Therefore, after a does not match x, the Operation stops, because lastIndexOf is already in the header.
If the string to be tested is very long, the efficiency of the two methods will be significantly different.
The same is true for endsWith.

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.