码迷,mamicode.com
首页 > Web开发 > 详细

JS性能举例(连接字符串)

时间:2016-04-17 09:08:16      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

    var startTime = new Date().valueOf();
    var s = "";
    for (var i = 0; i < 10000; i++) {//用加号进行连接
      s += "This is a test string";
    }
    return new Date().valueOf() - startTime;
  }

  function goodTest() {
    var startTime = new Date().valueOf();
    var stringBuffer = new Array();
    for (var i = 0; i < 10000; i++) {
      stringBuffer.push("This is a test string");//用数组自带的push()方法
    }
    var s = stringBuffer.join("");
    return new Date().valueOf() - startTime;
  }

  function betterTest() {
    var startTime = new Date().valueOf();
    var stringBuffer = new Array();
    for (var i = 0; i < 10000; i++) {
      stringBuffer[stringBuffer.length] = "This is a test string";
    }
    var s = stringBuffer.join("");
    return new Date().valueOf() - startTime;
  }

 document.write(badTest()+"<br />");
 document.write(goodTest()+"<br />");
 document.write(betterTest()+"<br />");

 

JS性能举例(连接字符串)

标签:

原文地址:http://www.cnblogs.com/feile/p/5400277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!