Prototype source analysis of the string part (i) of the relevant indexof optimization _prototype

Source: Internet
Author: User

There are a number of ways to add to string.prototype, but it boils down to roughly the following categories:

Classification Method name
Original capacity 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 with a basic initial capacity enhancement, the following is a concrete implementation, which is well understood:

Copy Code code 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 upper strip is $.trim in jquery, and most seem to be trim. Here the tragedy of the direct expansion of the original prototype appears, because the next JS implementation (such as chrome) to achieve the trim method, it is self-defeating.
Copy Code code as follows:

function strip () {
Return This.replace (/^\s+/, '). Replace (/\s+$/, ");
}

The Replace (/^\s+/, "") in this is Trimleft,replace (/\s+$/, "") is TrimRight, but the prototype.string does not have these two methods.

Here are some of the more interesting parts of this section:

At that time to see this paragraph, the startswith and EndsWith is very puzzled, logically speaking, startswith with IndexOf can, here is used LastIndexOf. Then I went through the Prototype1.6 version of the implementation:
Copy Code code 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;
}

Visible, the previous version of StartsWith used is indexof, but the 1.7 version of the StartsWith to modify the implementation. In version 1.7:

In the StartsWith implementation, LastIndexOf looks forward from the back, but the starting point (Fromindex) is set to 0, so it only needs to be detected at the beginning.
In the EndsWith implementation, IndexOf looks backwards, because the length of the string is variable, so the length is computed, and then the starting point (Fromindex) is determined, so it is only necessary to detect the end time.

The performance optimization here is that in the 1.6 implementation, if the beginning does not match (that is, startswith), the indexof still looks backwards until a match or end of the string is found, which is wasted. For example, for one of the following actions:

' ABCDEFGABCDEFG '. StartsWith (' abc ')
There is no difference between the 1.6 version and the 1.7 implementation, but let's switch to:

' ABCDEFGABCDEFG '. StartsWith (' Xesam ')
In the 1.6 implementation, the indexof operation within the StartsWith will not match x after the beginning of a, although there is no need to continue, but IndexOf will continue to look backwards until it finds a matching ' xesam ' or end of the string.
In the 1.7 implementation, the LastIndexOf inside the startswith is a reverse lookup (fromindex=0), so the operation stops when the beginning of a does not match the X, because LastIndexOf is already over.
In contrast, the efficiency of the two implementations can be significantly different if the string to be detected is very long.
The principle of endswith is the same.

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.