A large number of string output performance test in JS

Source: Internet
Author: User

Objective: To obtain a simple and efficient method of including 20 "<li></li>".

When I heard the need, the following method came into my mind:

The code is as follows Copy Code

function gethtmlbystring (len) {
var html = "";
for (var i = 0; i < len; i++) {
HTML + "<li></li>";
return HTML;

However, since the girl is deliberately asking questions, presumably the answer will not be so simple. So I changed the angle, and came to the following method:

The code is as follows Copy Code

function Gethtmlbyloop (len) {
var arr = [];
for (var i = 0; i < len; i++) {
Arr.push ("<li></li>");
Return Arr.join ("");

I'm full of confidence, thinking that's the best answer. But you don't want to, girl? Serena tells me that she knows a way to do it with just a line of short code. Although my heart is unwilling, but more is the curiosity overflow, can not help to cross-examine the ideal answer should be what?

The code is as follows Copy Code
function Gethtmlbyarray (len) {
return new Array (len + 1). Join ("<li></li>");

I listen, perhaps in other people's view it is simple can not be easy to answer, but I was dumbfounded, because I really do not know, the original really can be so simple!!

Today suddenly whim, want to verify the so-called optimal method, is really the best! So, a simple validation example was written:

  code is as follows copy code

var T1, T2, T3, T4, str1, str2, STR3;
T1 = new Date ();
str1 = gethtmlbystring (100000);
T2 = new Date ();
str2 = Gethtmlbyloop (100000);
T3 = new Date ();
Str3 = Gethtmlbyarray (100000);
T4 = new Date ();
Console.log ("str1 = = str2->" + (str1 = = str2));
Console.log ("str2 = = Str3->" + (str2 = = STR3));
Console.log ("t2-t1 =" + (T2-T1), "nt3-t2 =" + (T3-T2), "NT4-T3 =" + (T4-T3));

Verifies the time consuming of three methods and whether the results are consistent. After the chrome, IE8 test, although the operating efficiency under IE is not too obvious, but combined with the amount of code and other factors, combined I fully accept the optimal answer!!! And the test results seem to overturn the conclusion that I have always thought that using Arr.join ("") efficiency is better than string stitching ...

Although this case is very special, it does not represent most scenarios. However, we can draw some conclusions from this case:

◦ Many ways not we don't know, but we didn't think
◦ When we're done with a task, and many seconds of thinking, we get better results.
◦ The number of code, can not determine the pros and cons of the function
◦ Maybe your skill mastery is so-so, but there's a lot of detail/Chine that you should be digging into.
Suddenly feel this is what other people's achievements to write their own articles, some of the moral integrity Ah, stop, over~

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.