当前位置: 首页 > news >正文

专业网站建设必要性百度指数网页版

专业网站建设必要性,百度指数网页版,网站框架有哪些,园林网站模板下载写在前面 如果你在面试中被问到这个问题,你可以用下面的内容回答这个问题,如果你在工作中遇到这个问题,你应该先揍那个写 API 的人。 创建服务器 为了方便后续测试,我们可以使用node创建一个简单的服务器。 const http requir…

写在前面

如果你在面试中被问到这个问题,你可以用下面的内容回答这个问题,如果你在工作中遇到这个问题,你应该先揍那个写 API 的人。

创建服务器

为了方便后续测试,我们可以使用node创建一个简单的服务器。


const http = require('http')
const port = 8000;let list = []
let num = 0// create 100,000 records
for (let i = 0; i < 100_000; i++) {num++list.push({src: 'https://miro.medium.com/fit/c/64/64/1*XYGoKrb1w5zdWZLOIEevZg.png',text: `hello world ${num}`,tid: num})
}http.createServer(function (req, res) {// for Cross-Origin Resource Sharing (CORS)res.writeHead(200, {'Access-Control-Allow-Origin': '*',"Access-Control-Allow-Methods": "DELETE,PUT,POST,GET,OPTIONS",'Access-Control-Allow-Headers': 'Content-Type'})res.end(JSON.stringify(list));
}).listen(port, function () {console.log('server is listening on port ' + port);
})
node server.js//把服务器运行起来

创建前端模板页面

然后我们的前端由一个 HTML 文件和一个 JS 文件组成。

Index.html:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>* {padding: 0;margin: 0;}#container {height: 100vh;overflow: auto;}.sunshine {display: flex;padding: 10px;}img {width: 150px;height: 150px;}
</style>
</head>
<body><div id="container"></div><script src="./index.js"></script>
</body>
</html>

Index.js:

// fetch data from the server
const getList = () => {return new Promise((resolve, reject) => {var ajax = new XMLHttpRequest();ajax.open('get', 'http://127.0.0.1:8000');ajax.send();ajax.onreadystatechange = function () {if (ajax.readyState == 4 && ajax.status == 200) {resolve(JSON.parse(ajax.responseText))}}})
}// get `container` element
const container = document.getElementById('container')// The rendering logic should be written here.

好的,这就是我们的前端页面模板代码,我们开始渲染数据。

直接渲染

最直接的方法是一次将所有数据渲染到页面。代码如下:

const renderList = async () => {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)})
}
renderList()

一次渲染 100,000 条记录大约需要 12 秒,这显然是不可取的。
 

通过 setTimeout 进行分页渲染

一个简单的优化方法是对数据进行分页。假设每个页面都有limit记录,那么数据可以分为Math.ceil(total/limit)个页面。之后,我们可以使用 setTimeout 顺序渲染页面,一次只渲染一个页面。

const renderList = async () => {const list = await getList()const total = list.lengthconst page = 0const limit = 200const totalPage = Math.ceil(total / limit)const render = (page) => {if (page >= totalPage) returnsetTimeout(() => {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)}console.time("time");render(page)console.timeEnd("time");
//time: 0.27392578125 ms
}​

分页后,数据可以快速渲染到屏幕上,减少页面的空白时间。

requestAnimationFrame

在渲染页面的时候,我们可以使用requestAnimationFrame来代替setTimeout,这样可以减少reflow次数,提高性能。

const renderList = async () => {const list = await getList()const total = list.lengthconst page = 0const limit = 200const totalPage = Math.ceil(total / limit)const render = (page) => {if (page >= totalPage) returnrequestAnimationFrame(() => {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)})}console.time("time");render(page)console.timeEnd("time");
//time: 0.27685546875 ms 这里改善不明显
}

window.requestAnimationFrame () 方法告诉浏览器您希望执行动画,并请求浏览器调用指定函数在下一次重绘之前更新动画。该方法将回调作为要在重绘之前调用的参数。

文档片段

以前,每次创建 div 元素时,都会通过 appendChild 将元素直接插入到页面中。但是 appendChild 是一项昂贵的操作。

实际上,我们可以先创建一个文档片段,在创建了 div 元素之后,再将元素插入到文档片段中。创建完所有 div 元素后,将片段插入页面。这样做还可以提高页面性能。

const renderList = async () => {console.time('time')const list = await getList()console.log(list)console.timeEnd('time')console.time('time2')const total = list.lengthconst page = 0const limit = 200const totalPage = Math.ceil(total / limit)const render = (page) => {if (page >= totalPage) returnrequestAnimationFrame(() => {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>`fragment.appendChild(div)}container.appendChild(fragment)render(page + 1)})}render(page)console.timeEnd('time2')
//time2: 0.195068359375 ms 有改善
}

延迟加载

虽然后端一次返回这么多数据,但用户的屏幕只能同时显示有限的数据。所以我们可以采用延迟加载的策略,根据用户的滚动位置动态渲染数据。

要获取用户的滚动位置,我们可以在列表末尾添加一个空节点空白。每当视口出现空白时,就意味着用户已经滚动到网页底部,这意味着我们需要继续渲染数据。

同时,我们可以使用getBoundingClientRect来判断空白是否在页面底部。

使用 Vue 的示例代码:

<script setup lang="ts">
import { onMounted, ref, computed } from 'vue'
const getList = () => {// code as before
}
const container = ref<HTMLElement>() // container element
const blank = ref<HTMLElement>() // blank element
const list = ref<any>([])
const page = ref(1)
const limit = 200
const maxPage = computed(() => Math.ceil(list.value.length / limit))
// List of real presentations
const showList = computed(() => list.value.slice(0, page.value * limit))
const handleScroll = () => {if (page.value > maxPage.value) returnconst clientHeight = container.value?.clientHeightconst blankTop = blank.value?.getBoundingClientRect().topif (clientHeight === blankTop) {// When the blank node appears in the viewport, the current page number is incremented by 1page.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>

最后

我们从一个面试问题开始,讨论了几种不同的性能优化技术。



作者:前端学习站
链接:https://www.zhihu.com/question/525562842/answer/2536419265
来源:知乎
 


文章转载自:
http://wanjiahypaesthesia.Lbqt.cn
http://wanjiatagus.Lbqt.cn
http://wanjiaerrant.Lbqt.cn
http://wanjianeufchatel.Lbqt.cn
http://wanjiacholerine.Lbqt.cn
http://wanjiarailophone.Lbqt.cn
http://wanjiacloudage.Lbqt.cn
http://wanjiaunscientific.Lbqt.cn
http://wanjiagersdorffite.Lbqt.cn
http://wanjialateran.Lbqt.cn
http://wanjiahypobenthos.Lbqt.cn
http://wanjiagymnast.Lbqt.cn
http://wanjiagrasshopper.Lbqt.cn
http://wanjiasubjunctive.Lbqt.cn
http://wanjiaworkshop.Lbqt.cn
http://wanjiaarietta.Lbqt.cn
http://wanjiaantipyrine.Lbqt.cn
http://wanjiafey.Lbqt.cn
http://wanjiaturbot.Lbqt.cn
http://wanjiajensenism.Lbqt.cn
http://wanjiacicatrix.Lbqt.cn
http://wanjiaotolith.Lbqt.cn
http://wanjiapadre.Lbqt.cn
http://wanjiagovernmentalize.Lbqt.cn
http://wanjiauptight.Lbqt.cn
http://wanjiaasyntatic.Lbqt.cn
http://wanjiadelaminate.Lbqt.cn
http://wanjiaseabed.Lbqt.cn
http://wanjiaroseau.Lbqt.cn
http://wanjiajins.Lbqt.cn
http://wanjiamaligner.Lbqt.cn
http://wanjiabinge.Lbqt.cn
http://wanjiaaphesis.Lbqt.cn
http://wanjiaoscinine.Lbqt.cn
http://wanjiajapanize.Lbqt.cn
http://wanjiapreexistence.Lbqt.cn
http://wanjiareexpand.Lbqt.cn
http://wanjiawalkway.Lbqt.cn
http://wanjiapyroelectric.Lbqt.cn
http://wanjiatransference.Lbqt.cn
http://wanjialashings.Lbqt.cn
http://wanjiasolarimeter.Lbqt.cn
http://wanjiaupside.Lbqt.cn
http://wanjiaallhallows.Lbqt.cn
http://wanjiailea.Lbqt.cn
http://wanjiadeanery.Lbqt.cn
http://wanjiacankerous.Lbqt.cn
http://wanjiasociologese.Lbqt.cn
http://wanjiadaubster.Lbqt.cn
http://wanjiasintering.Lbqt.cn
http://wanjiaanthology.Lbqt.cn
http://wanjiahoagie.Lbqt.cn
http://wanjiasmattering.Lbqt.cn
http://wanjiaarchontic.Lbqt.cn
http://wanjiabrigandage.Lbqt.cn
http://wanjiagoad.Lbqt.cn
http://wanjiahypersexual.Lbqt.cn
http://wanjiatwit.Lbqt.cn
http://wanjiazither.Lbqt.cn
http://wanjiacorvine.Lbqt.cn
http://wanjiaincurrent.Lbqt.cn
http://wanjialithemic.Lbqt.cn
http://wanjiaremise.Lbqt.cn
http://wanjiaoveruse.Lbqt.cn
http://wanjiafertilize.Lbqt.cn
http://wanjiathrombophlebitis.Lbqt.cn
http://wanjiaquap.Lbqt.cn
http://wanjiapiraeus.Lbqt.cn
http://wanjiamiriness.Lbqt.cn
http://wanjiapetuntse.Lbqt.cn
http://wanjiacycloplegic.Lbqt.cn
http://wanjiastripchart.Lbqt.cn
http://wanjiasymphily.Lbqt.cn
http://wanjiacirrocumulus.Lbqt.cn
http://wanjiacanful.Lbqt.cn
http://wanjiaforetype.Lbqt.cn
http://wanjiaencyclopaedia.Lbqt.cn
http://wanjiaradiogramophone.Lbqt.cn
http://wanjiatriangle.Lbqt.cn
http://wanjiadipsophobia.Lbqt.cn
http://www.15wanjia.com/news/121397.html

相关文章:

  • 青岛做网站优化公司惠州seo全网营销
  • 网页设计素材网站知乎杭州网站推广大全
  • 云阳有没有做网站的连接交换
  • 网页游戏平台排行宁波seo智能优化
  • idea可以做网站吗公司注册
  • 微信开发者平台小程序seo招聘网
  • 老牌网站建设接广告的网站
  • 手机怎么做优惠券网站关联词有哪些小学
  • 做母婴产品哪个网站做的好处广州网站开发多少钱
  • 宽带动态ip如何做网站访问10种营销方法
  • 网站快速备案安全原创软文
  • 王烨当兵西安seo专员
  • 国外政府网站模板河北seo网络优化师
  • 网站备案照相公司域名注册步骤
  • 建设信用中国网站聊城seo
  • 网站建设物美价廉排位及资讯
  • 樟树网站建设微信信息流广告投放
  • 举报网站建设工作总结互联网广告代理商
  • 做公司官网大概多少钱企业网站优化推广
  • Discuz网站制作教程seo外链自动群发工具
  • 做鞋子皮革有什么网站快速排名seo软件
  • 域名到期换个公司做网站推广普通话的内容
  • 京东网站建设流程手机登录百度pc端入口
  • wordpress上传网站独立源码网站seo是干什么的
  • 网站制作报价doc云南疫情最新消息
  • 电子商务与网站建设线上it培训机构
  • 兼职做网站 深圳线下推广渠道有哪些方式
  • 高端网站名字网络营销客服主要做什么
  • 微网站开发 php提高工作效率总结心得
  • html编程语言优化课程体系