javascript如何测试字符串的拼接效率呢?


摘要:
下文讲述使用js代码拼接字符串的效率分析,如下所示:

实现思路:
    通过计算字符串拼接完成的时间戳减去字符串拼接开始的时间戳

例:


    <meta charset="utf-8" />
    <title>猫猫教程---字符串拼接示例分享</title>
    <p>
     maomao365.com <br />
     字符串拼接的效率分析
    </p>
    <hr />
    <script>
        var start = new Date();
        var str = "";
        for (var i = 0; i < 1000000; i++) {
            str += "test";
        }
        var end = new Date();
        document.writeln("+拼接字符串,耗时:" + (end.getMilliseconds() - start.getMilliseconds()));
        document.writeln("<br/>");
        var begin = new Date();
        var arry = new Array();
        for (var i = 0; i < 1000000; i++) {
            arry.push("test");
        }
        arry.join("");
        var stop = new Date();
        document.writeln("数组方式拼接字符串,耗时:" + (stop.getMilliseconds() - begin.getMilliseconds()));
    </script>