前端优化-代码层面

<script type="text/javascript">
    console.time(`test`)
    var len = 100
    var ul = document.getElementById('list')
    for (var i = 0; i < len; i++) {
        var li = document.createElement('li')
        ul.innerHTML += `<li>${i}</li>`
    }
    
    console.timeEnd(`test`)
    ul.innerHTML = ''
    
    console.time(`test1`)
    var str = ''
    var ul = document.getElementById('list')
    for (var i = 0; i < len; i++) {
        str += `<li>${i}</li>`
    }
    ul.innerHTML += str
    console.timeEnd(`test1`)
</script>

执行结果如下:相差巨大

posted @ 2019-10-18 18:01  空山与新雨  阅读(198)  评论(0编辑  收藏  举报