vue前端優(yōu)雅展示后端十萬(wàn)條數(shù)據(jù)面試點(diǎn)剖析
前置工作
如果后端真的返回給前端10萬(wàn)條數(shù)據(jù),咱們前端要怎么優(yōu)雅地展示出來(lái)呢?(哈哈假設(shè)后端真的能傳10萬(wàn)條數(shù)據(jù)到前端)
先把前置工作給做好,后面才能進(jìn)行測(cè)試
后端搭建
新建一個(gè)server.js
文件,簡(jiǎn)單起個(gè)服務(wù),并返回給前端10w
條數(shù)據(jù),并通過(guò)nodemon server.js
開(kāi)啟服務(wù)
沒(méi)有安裝nodemon
的同學(xué)可以先全局安裝
npm i nodemon -g
// server.js const http = require('http') const port = 8000; http.createServer(function (req, res) { // 開(kāi)啟Cors res.writeHead(200, { //設(shè)置允許跨域的域名,也可設(shè)置*允許所有域名 'Access-Control-Allow-Origin': '*', //跨域允許的請(qǐng)求方法,也可設(shè)置*允許所有方法 "Access-Control-Allow-Methods": "DELETE,PUT,POST,GET,OPTIONS", //允許的header類型 'Access-Control-Allow-Headers': 'Content-Type' }) let list = [] let num = 0 // 生成10萬(wàn)條數(shù)據(jù)的list for (let i = 0; i < 100000; i++) { num++ list.push({ src: 'https://p3-passport.byteacctimg.com/img/user-avatar/d71c38d1682c543b33f8d716b3b734ca~300x300.image', text: `我是${num}號(hào)嘉賓林三心`, tid: num }) } res.end(JSON.stringify(list)); }).listen(port, function () { console.log('server is listening on port ' + port); })
前端頁(yè)面
先新建一個(gè)index.html
// index.html // 樣式 <style> * { padding: 0; margin: 0; } #container { height: 100vh; overflow: auto; } .sunshine { display: flex; padding: 10px; } img { width: 150px; height: 150px; } </style> // html部分 <body> <div id="container"> </div> <script src="./index.js"></script> </body>
然后新建一個(gè)index.js
文件,封裝一個(gè)AJAX
函數(shù),用來(lái)請(qǐng)求這10w
條數(shù)據(jù)
// index.js // 請(qǐng)求函數(shù) const getList = () => { return new Promise((resolve, reject) => { //步驟一:創(chuàng)建異步對(duì)象 var ajax = new XMLHttpRequest(); //步驟二:設(shè)置請(qǐng)求的url參數(shù),參數(shù)一是請(qǐng)求的類型,參數(shù)二是請(qǐng)求的url,可以帶參數(shù) ajax.open('get', 'http://127.0.0.1:8000'); //步驟三:發(fā)送請(qǐng)求 ajax.send(); //步驟四:注冊(cè)事件 onreadystatechange 狀態(tài)改變就會(huì)調(diào)用 ajax.onreadystatechange = function () { if (ajax.readyState == 4 && ajax.status == 200) { //步驟五 如果能夠進(jìn)到這個(gè)判斷 說(shuō)明 數(shù)據(jù) 完美的回來(lái)了,并且請(qǐng)求的頁(yè)面是存在的 resolve(JSON.parse(ajax.responseText)) } } }) } // 獲取container對(duì)象 const container = document.getElementById('container')
直接渲染
最直接的方式就是直接渲染出來(lái),但是這樣的做法肯定是不可取的,因?yàn)橐淮涡凿秩境?code>10w個(gè)節(jié)點(diǎn),是非常耗時(shí)間的,咱們可以來(lái)看一下耗時(shí),差不多要消耗12秒
,非常消耗時(shí)間
const renderList = async () => { console.time('列表時(shí)間') const list = await getList() list.forEach(item => { const div = document.createElement('div') div.className = 'sunshine' div.innerHTML = `<img src="${item.src}" /><span>${item.text}</span>` container.appendChild(div) }) console.timeEnd('列表時(shí)間') } renderList()
setTimeout分頁(yè)渲染
這個(gè)方法就是,把10w
按照每頁(yè)數(shù)量limit
分成總共Math.ceil(total / limit)
頁(yè),然后利用setTimeout
,每次渲染1頁(yè)數(shù)據(jù),這樣的話,渲染出首頁(yè)數(shù)據(jù)的時(shí)間大大縮減了
const renderList = async () => { console.time('列表時(shí)間') const list = await getList() console.log(list) const total = list.length const page = 0 const limit = 200 const totalPage = Math.ceil(total / limit) const render = (page) => { if (page >= totalPage) return setTimeout(() => { for (let i = page * limit; i < page * limit + limit; i++) { const item = list[i] const div = document.createElement('div') div.className = 'sunshine' div.innerHTML = `<img src="${item.src}" /><span>${item.text}</span>` container.appendChild(div) } render(page + 1) }, 0) } render(page) console.timeEnd('列表時(shí)間') }
requestAnimationFrame
使用requestAnimationFrame
代替setTimeout
,減少了重排的次數(shù),極大提高了性能,建議大家在渲染方面多使用requestAnimationFrame
const renderList = async () => { console.time('列表時(shí)間') const list = await getList() console.log(list) const total = list.length const page = 0 const limit = 200 const totalPage = Math.ceil(total / limit) const render = (page) => { if (page >= totalPage) return // 使用requestAnimationFrame代替setTimeout requestAnimationFrame(() => { for (let i = page * limit; i < page * limit + limit; i++) { const item = list[i] const div = document.createElement('div') div.className = 'sunshine' div.innerHTML = `<img src="${item.src}" /><span>${item.text}</span>` container.appendChild(div) } render(page + 1) }) } render(page) console.timeEnd('列表時(shí)間') }
文檔碎片 + requestAnimationFrame
文檔碎片的好處
1、之前都是每次創(chuàng)建一個(gè)div
標(biāo)簽就appendChild
一次,但是有了文檔碎片可以先把1頁(yè)的div
標(biāo)簽先放進(jìn)文檔碎片
中,然后一次性appendChild
到container
中,這樣減少了appendChild
的次數(shù),極大提高了性能
2、頁(yè)面只會(huì)渲染文檔碎片包裹著的元素,而不會(huì)渲染文檔碎片
const renderList = async () => { console.time('列表時(shí)間') const list = await getList() console.log(list) const total = list.length const page = 0 const limit = 200 const totalPage = Math.ceil(total / limit) const render = (page) => { if (page >= totalPage) return requestAnimationFrame(() => { // 創(chuàng)建一個(gè)文檔碎片 const fragment = document.createDocumentFragment() for (let i = page * limit; i < page * limit + limit; i++) { const item = list[i] const div = document.createElement('div') div.className = 'sunshine' div.innerHTML = `<img src="${item.src}" /><span>${item.text}</span>` // 先塞進(jìn)文檔碎片 fragment.appendChild(div) } // 一次性appendChild container.appendChild(fragment) render(page + 1) }) } render(page) console.timeEnd('列表時(shí)間') }
懶加載
為了比較通俗的講解,咱們啟動(dòng)一個(gè)vue
前端項(xiàng)目,后端服務(wù)還是開(kāi)著
其實(shí)實(shí)現(xiàn)原理很簡(jiǎn)單,咱們通過(guò)一張圖來(lái)展示,就是在列表尾部放一個(gè)空節(jié)點(diǎn)blank
,然后先渲染第1頁(yè)數(shù)據(jù),向上滾動(dòng),等到blank
出現(xiàn)在視圖中,就說(shuō)明到底了,這時(shí)候再加載第二頁(yè),往后以此類推。
至于怎么判斷blank
出現(xiàn)在視圖上,可以使用getBoundingClientRect
方法獲取top
屬性
IntersectionObserver
性能更好,但是我這里就拿getBoundingClientRect
來(lái)舉例
<script setup lang="ts"> import { onMounted, ref, computed } from 'vue' const getList = () => { // 跟上面一樣的代碼 } const container = ref<HTMLElement>() // container節(jié)點(diǎn) const blank = ref<HTMLElement>() // blank節(jié)點(diǎn) const list = ref<any>([]) // 列表 const page = ref(1) // 當(dāng)前頁(yè)數(shù) const limit = 200 // 一頁(yè)展示 // 最大頁(yè)數(shù) const maxPage = computed(() => Math.ceil(list.value.length / limit)) // 真實(shí)展示的列表 const showList = computed(() => list.value.slice(0, page.value * limit)) const handleScroll = () => { // 當(dāng)前頁(yè)數(shù)與最大頁(yè)數(shù)的比較 if (page.value > maxPage.value) return const clientHeight = container.value?.clientHeight const blankTop = blank.value?.getBoundingClientRect().top if (clientHeight === blankTop) { // blank出現(xiàn)在視圖,則當(dāng)前頁(yè)數(shù)加1 page.value++ } } onMounted(async () => { const res = await getList() list.value = res }) </script> <template> <div id="container" @scroll="handleScroll" ref="container"> <div class="sunshine" v-for="(item) in showList" :key="item.tid"> <img :src="item.src" /> <span>{{ item.text }}</span> </div> <div ref="blank"></div> </div> </template>
虛擬列表
虛擬列表需要講解的比較多,在這里我分享一下我的一篇虛擬列表的文章
以上就是vue前端優(yōu)雅展示后端十萬(wàn)條數(shù)據(jù)考點(diǎn)剖析的詳細(xì)內(nèi)容,更多關(guān)于前端展示后端數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
VUE子組件向父組件傳值詳解(含傳多值及添加額外參數(shù)場(chǎng)景)
這篇文章主要給大家介紹了關(guān)于VUE子組件向父組件傳值(含傳多值及添加額外參數(shù)場(chǎng)景)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09前端XSS攻擊場(chǎng)景詳解與Vue.js處理XSS的方法(vue-xss)
這篇文章主要給大家介紹了關(guān)于前端XSS攻擊場(chǎng)景與Vue.js使用vue-xss處理XSS的方法,介紹了實(shí)際工作中渲染數(shù)據(jù)時(shí)遇到XSS攻擊時(shí)的防范措施,以及解決方案,需要的朋友可以參考下2024-02-02vue3中keep-alive和vue-router的結(jié)合使用方式
這篇文章主要介紹了vue3中keep-alive和vue-router的結(jié)合使用方式,?具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue中傳遞自定義參數(shù)到后端、后端獲取數(shù)據(jù)并使用Map接收參數(shù)
有些傳遞的參數(shù)是直接拼接到URL地址欄中的、但是為了統(tǒng)一管理、不能將傳遞的參數(shù)直接拼接到地址欄中,接下來(lái)通過(guò)本文給大家介紹Vue中傳遞自定義參數(shù)到后端、后端獲取數(shù)據(jù)并使用Map接收參數(shù),感興趣的朋友一起看看吧2022-10-10Vue 實(shí)現(xiàn)樹(shù)形視圖數(shù)據(jù)功能
這篇文章主要介紹了Vue 實(shí)現(xiàn)樹(shù)形視圖數(shù)據(jù)功能,利用簡(jiǎn)單的樹(shù)形視圖實(shí)現(xiàn)的,在實(shí)現(xiàn)過(guò)程中熟悉了組件的遞歸使用,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05elementui?el-table底層背景色修改簡(jiǎn)單方法
最近在做項(xiàng)目的時(shí)候遇到個(gè)需求,需要修改el-table背景色,這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于elementui?el-table底層背景色修改的相關(guān)資料,需要的朋友可以參考下2023-10-10每天學(xué)點(diǎn)Vue源碼之vm.$mount掛載函數(shù)
這篇文章主要介紹了vm.$mount掛載函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03vue-quill-editor+plupload富文本編輯器實(shí)例詳解
這篇文章主要介紹了vue-quill-editor+plupload富文本編輯器實(shí)例詳解,需要的朋友可以參考下2018-10-10vue + element ui實(shí)現(xiàn)播放器功能的實(shí)例代碼
這篇文章主要介紹了vue + element ui實(shí)現(xiàn)播放器功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04