パフォーマンスの最適化:仮想DOM千万データをレンダリングする方法をリスト、ページカトンなし

最近では、いくつかの低備えたコンピュータ上のページ20000の直接レンダリングデータはこの要求に基づいて、立ち往生ページに輝く可能性がある場合約20,000データのリストは、ページを作成することはできませんでした需要を、作られました仮想手書きリストへ

考え

  1. など60のように少量のデータだけ固定表示リスト、
  2. DOMをスクロールしたとき、リストから削除挿入するようにしてください
  3. startIndex、endIndexが、常に最新のディスプレイリストを取得するには、これを変更します
  4. コンテナのpaddingTop、paddingBottomの伸延スクロール領域

20000のリストに直接接続する場合、ページのパフォーマンスを初めて目

あなたは赤い炎の姿を見ることができますが、DOMレンダリングの一部となっているにも時間がかかりますが、また1秒以上

仮想リストを用いたものをするとき、ページのパフォーマンスを見て

図炎から分かるように、仮想ページのパフォーマンスのリストによってレンダリングされる証明緑色炎図は、大幅に改善されています

簡単なデモは、仮想リストを実装します

我々は、容器、600PX高さ、各リスト項目の30px高さがあると仮定し、我々は、この時点で、我々は、容器を与えることができ、また、60のデータ表示の高さを知ることができるリストの長さに応じてロール容器の全体の高さを算出することができますプラス伸延コンテナにpaddingBottomのは、スクロールする必要があるページの高さをシミュレートします

this.paddingBottom = this.allHeight - this.scrollList.length * 30

コンテナは、データの先頭を圧延後に削除されたときにコンテナがまたpaddingTopを使用する必要がscrollTopスプライトをホールドアップ

最後に、我々は一定の変化にイベントリスナーコンテナをスクロールする必要がpaddingTop、paddingBottomの、startIndexの、endIndexの

最終結果

最後に、すべてのコードを添付

<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .container {
            width: 300px;
            height: 600px;
            overflow: auto;
            border: 1px solid;
            margin: 100px auto;
        }
        .item {
            height: 29px;
            line-height: 30px;
            border-bottom: 1px solid #aaa;
            padding-left: 20px;
        }
    </style>
</head>
<body>
<div id="app">
    <button @click="add">增加</button>
    <div class="container" ref="container">
        <div class="scroll-wrapper" :style="style">
            <div v-for="(item, index) in scrollList" :key="index" class="item">{{item}}</div>
        </div>
    </div>
</div>
<script src="./vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            list: [
                '测试数据'
            ],
            startIndex: 0,
            endIndex: 60,
            paddingTop: 0,
            paddingBottom: 0,
            allHeight: 0
        },
        computed: {
            scrollList() {
                return this.list.slice(this.startIndex, this.endIndex)
            },
            style() {
                return {
                    paddingTop: this.paddingTop + 'px',
                    paddingBottom: this.paddingBottom + 'px'
                }
            }
        },
        watch: {
            list(val) {
                const valLen = val.length
                this.allHeight = valLen * 30
                this.paddingBottom = this.allHeight - this.scrollList.length * 30
            }
        },
        mounted() {
            const container = this.$refs.container
            container.addEventListener('scroll', () => {
                const top = container.scrollTop
                this.startIndex = Math.floor(top / 30)
                this.endIndex = this.startIndex + 60

                this.paddingTop = top
                if (this.endIndex >= this.list.length - 1) {
                    this.paddingBottom = 0
                    return
                }
                this.paddingBottom = this.allHeight - 600 - top
            })
        },
        methods: {
            add() {
                let arr = new Array(50000).fill(0)
                arr = arr.map((item, index) => {
                    return index
                })
                this.list = [
                    ...this.list,
                    ...arr
                ]
            }
        }
    })
</script>
</body>
</html>

おすすめ

転載: www.cnblogs.com/songbw/p/11613869.html