性能优化总结
css 和 js 优化
- 减少
css
和js
阻塞;
- 优化样式表的代码(选择器的权重);
- 异步加载js( script 标签加上 defer 属性),放在页面底部;
- 对
css
、js
进行压缩;
- 减少http请求数,将多个
css
、js
文件合并。
使用CDN节点进行外部资源加速。
图片优化
- 使用字体图标代替图片图标
- 图片使用CSS雪碧图
- 图片懒加载
- 响应式图片。根据屏幕大小自动加载合适的图片
- 图片压缩
减少重绘和回流
- 元素位置移动变换时尽量使用transform
- 用opacity来代替visibility(opacity配合图层使用,既不触发重绘也不触发重排)
- 将多次改变样式属性的操作合并成一次操作。(预先定义好class,然后修改DOM的className)
- 将DOM离线后再修改(先隐藏它,操作完成后再显示,这样只在隐藏和显示时触发2次重排)
- 利用文档碎片。(documentFragment)
- 动画实现过程中,启用GPU硬件加速:transform: tranlateZ(0)。
- 使用requestAnimationFrame(cb) (请求动画帧:回调函数会在浏览器下一次重绘之前调用。)
使用事件委托。(利用事件冒泡,可以管理某一类型的所有事件,可以节省内存)
强缓存
- Expires
- cache-control: max-age = number
协商缓存
- Last-Modified/If-Modified-Since
- Etag/If-None-Match
Storage
资源预加载
- prefetch 是利用浏览器的空闲时间,加载页面将来可能用到的资源的一种机制;通常可以用于加载其他页面(非首页)所需要的资源,以便加快后续页面的打开速度
- 在后台预加载具有高优先级的资源,对于在几秒钟内预加载您需要的资源非常有用
webpack打包优化
**alias
**:起别名,减少查找过程
后缀尝试列表要尽可能的小,导入文件时尽可能的带上后缀,避免寻找过程(extensions)
include
缩小打包范围
Gzip
对文件进行压缩,能大大提高首屏加载速度
总体优化
- 异步加载或按需加载
- 路由懒加载(实现:ES6动态加载模块import() )
- 服务端渲染。提高首屏加载速度。
DNS预解析。 适用于网页引用了大量其他域名的资源
Web Worker
- 作用就是为 JavaScript 创造多线程环境,将一些任务在后台运行。
- Worker 线程完成计算任务,再把结果返回给主线程。
- 好处是一些计算密集型或高延迟的任务,被 Worker 线程负担了,主线程(通常负责 UI 交互)就会很流畅,不会被阻塞或拖慢。
-
Post title: 性能优化总结
-
Post author: Chao
-
Create time: 2019-07-10 14:45:00
-
Post link: 2019/07/10/性能优化总结/
-
Copyright notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.
$tools-item-width = 2.2rem
$tools-item-font-size = 1.1rem
$tools-item-border-radius = 0.1rem
.side-tools-container {
position relative
.tools-item {
width $tools-item-width
height $tools-item-width
margin-bottom 0.2rem
color var(--text-color-3)
font-size $tools-item-font-size
background var(--background-color-1)
border-right none
border-radius $tools-item-border-radius
box-shadow 0.1rem 0.1rem 0.2rem var(--shadow-color)
cursor pointer
i {
color var(--text-color-3)
}
&:hover {
color var(--background-color-1)
background var(--primary-color)
box-shadow 0.2rem 0.2rem 0.4rem var(--shadow-color)
i {
color var(--background-color-1)
}
}
+keep-tablet() {
width $tools-item-width * 0.9
height $tools-item-width * 0.9
margin-bottom 0.2rem
font-size $tools-item-font-size * 0.9
}
&.rss {
a {
width 100%
height 100%
border-radius $tools-item-border-radius
&:hover {
color var(--background-color-1)
background var(--primary-color)
box-shadow 0.2rem 0.2rem 0.4rem var(--shadow-color)
}
}
}
}
.side-tools-list {
transform translateX(100%)
opacity 0
transition-t("transform, opacity", "0, 0", "0.2, 0.2", "linear, linear")
&.show {
transform translateX(0)
opacity 1
}
}
.exposed-tools-list {
if (hexo-config('style.scroll.percent') == true) {
.tool-scroll-to-top {
display none
&.show {
display flex
}
&:hover {
.percent {
display none
}
.arrow-up {
display flex
}
}
.arrow-up {
display none
}
.percent {
display flex
font-size 1rem
}
}
}
}
}
$icon-size = 1.2rem
$search-header-height = 3rem
.search-pop-overlay {
position fixed
top 0
left 0
z-index $z-index-8
display flex
width 100%
height 100%
background rgba(0, 0, 0, 0)
visibility hidden
transition-t("visibility, background", "0, 0", "0.3, 0.3", "ease, ease")
&.active {
background rgba(0, 0, 0, 0.35)
visibility visible
.search-popup {
transform scale(1)
}
}
.search-popup {
z-index $z-index-6
width 70%
height 80%
margin auto
background var(--background-color-1)
border-radius 0.4rem
transform scale(0)
transition-t("transform", "0", "0.3", "ease")
+keep-tablet() {
width 80%
}
+keep-mobile() {
width 90%
}
.search-header {
display flex
align-items center
height $search-header-height
padding 0 1rem
background var(--text-color-6)
border-top-left-radius 0.2rem
border-top-right-radius 0.2rem
.search-input-field-pre {
margin-right 0.2rem
color var(--text-color-3)
font-size 1.3rem
cursor pointer
}
.search-input-container {
flex-grow 1
padding 0.2rem
.search-input {
width 100%
color var(--text-color-3)
font-size 1.2rem
background transparent
border 0
outline 0
&::-webkit-search-cancel-button {
display none
}
&::-webkit-input-placeholder {
color var(--text-color-4)
font-size 1rem
}
}
}
.close-popup-btn {
color var(--text-color-3)
font-size $icon-size
cursor pointer
&:hover {
color var(--text-color-1)
}
}
}
#search-result {
position relative
display flex
box-sizing border-box
height 'calc(100% - %s)' % $search-header-height
padding 0.3rem 1.5rem
overflow auto
.search-result-list {
width 100%
height 100%
font-size 1rem
li {
box-sizing border-box
margin 0.8rem 0
padding 0.8rem 0
border-bottom 0.1rem dashed var(--border-color)
&:last-child {
border-bottom none
}
.search-result-title {
position relative
display flex
align-items center
margin-bottom 0.8rem
padding-left 1rem
font-weight bold
&::after {
position absolute
top 50%
left 0
width 0.4rem
height 0.4rem
background var(--text-color-3)
border-radius 50%
transform translateY(-50%)
content ''
}
}
.search-result {
margin 0
padding-left 1rem
line-height 2rem
word-wrap break-word
}
a {
&:hover {
color var(--text-color-3)
}
}
.search-keyword {
color var(--primary-color)
font-weight bold
border-bottom 0.1rem dashed var(--primary-color)
}
}
}
#no-result {
margin auto
color var(--text-color-4)
}
}
}
}