Performance comparison of js string splicing in different browsers

<!DOCTYPE>
<html>
<head>
    <title></title>
</head>
<body>

</body>
</html>
<script type="text/javascript">
    var t1 = new Date().getTime();

    var string = '';
    for(i=1; i<=500000; i++){
        string += i;
    }
    var t2 = new Date().getTime();
    console.log(t2-t1);

    var arr = [];
    for(i=1; i<=500000; i++){
        arr[i] = i;
    }
    var t3 = new Date().getTime();
    arr.join();
    var t4 = new Date().getTime();
    console.log(t4-t3);
</script>

Conclusion
Chrome += is faster than arr.join() while IE and Firefox arr.join() is faster than +=
Chrome += is about 8 times faster than arr.join()
IE: arr.join() is 3 times faster than +=
Firefox: arr.join() is 3 times faster than += Firefox
is 10 times faster than IE
Most people use a little more IE, so in js projects, js processing strings using arr.join will be better than +=

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326030465&siteId=291194637