Javascript string Multiplication

Source: Internet
Author: User

Address: http://www.davidflanagan.com/2009/08/string-multipli.html
Original Author: David Flanagan
In Ruby, the "*" operator uses a string as the left parameter and a number as the right parameter to implement string duplication. For example, the value of "Ruby" * 2 is "rubyruby ". This is useful only in a few places (for example, generating a table consisting of ASCII characters such as hyphens), but it is very concise. It is much less efficient than writing a loop to connect strings for n times.

I just found that there is a clever technique in JavaScript to implement string multiplication:

CopyCode The Code is as follows: String. Prototype. Times = function (n ){
Return array. Prototype. Join. Call ({length: n + 1}, this );
};
"JS". Times (5) // => "jsjsjsjsjs"

This method calls an array. Join () action of an array whose elements are all "undefined. However, it does not actually create an array containing n + 1 "undefined" elements. It uses an anonymous object that contains the Length attribute and relies on the original function join () of the array object (). Because "object" is not an array and cannot directly call join (), it must be implemented through the call () of the prototype. A simple version with the same effect is provided below:

Copy code The Code is as follows: String. Prototype. Times = function (n) {return (new array (n + 1). Join (this );};

When we call the constructor with a parameter of array, we only set the length of the array. In fact, no elements of the array are created.

I only tested it in Firefox. I guess it will be more effective than a normal loop, But I didn't perform a benchmark test.

Author Profile
David Flanagan is a computer obsessed with Java writing.ProgramHe spent most of his time writing Java-related books. David earned a computer science degree in engineering at the Massachusetts Institute of Technology. He lives on the Pacific Northwest Coast between Seattle and Vancouver. He published best-selling books in o'reilly, including Java in a nutshell, Java foundation classes in a nutshell, Java Enterprise in a nutshell, and javascript: the definitive guide, JavaScript pocket reference, and the ruby programming language.

My comments
If efficiency is to be considered, it may be more efficient to slightly optimize loop iterations. For example, the following recursive call,AlgorithmThe complexity is O (log2n ). In Google Chrome, the test result is faster than that of David, but I have to admit that his method is elegant!Copy codeThe Code is as follows: String. Prototype. Times = function (n ){
If (n = 1 ){
Return this;
}
VaR midres = This. Times (math. Floor (n/2 ));
Midres + = midres;
If (N % 2 ){
Midres + = this;
}
Return midres;
}

Postscript
David adopted my suggestion and wrote a non-recursive version for us. See his blog article: http://www.davidflanagan.com/2009/08/good-algorithms.html.
Contact info
My mailbox, welcome letter (redraiment@gmail.com)

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.