一、前言
二、优化性能诊断工具
三、启动性能优化
{picFilter(item || '//:0','md', pictureRatio)
picFilter(item || '//:0','lg', pictureRatio)
<swiper-item> <image wx:if="{{index === 0 && !hasLoad}}" bind:load="loadImg" binderror="loadImg" catchtap="showBannerPic" data-picarr="{{item}}" data-picsrc="{{item}}" src="{{picFilter(item || '//:0', goodsInfo.imagePx || 'md', 10)}}" class="slide-image slide-first-picture" mode="aspectFill" ></image> <image wx:elif="{{ getShow(index, loopIndex, goodsInfo.goodsVideoUrl) }}" catchtap="showBannerPic" data-picarr="{{goodsInfo.goodsImageUrl}}" data-picsrc="{{item}}" src="{{picFilter(item || '//:0','lg', pictureRatio)}}" class="slide-image" mode="aspectFill" ></image></swiper-item><image wx:if="{{ goodsInfo.goodsImageUrl.length > 0 && isLoadBg }}" bind:load="loadEvent" binderror="loadEvent" src="{{picFilter(goodsInfo.goodsImageUrl[0] || '//:0','lg', pictureRatio)}}" mode="aspectFill" class="hide-image slide-image"></image>
loadImg(evt) { // 加载看不见的图片,预加载 this.setData({ isLoadBg: true, }); }, loadEvent() { // 预加载完成,setData this.setData({ hasLoad: true, }); },
{
"lazyCodeLoading": "requiredComponents"
}
{
"usingComponents": {
"pay-payment": "package://payment_cashier/pay-payment",
"browse-log-collector": "platform://ec/browseLogCollector",
"design-sdk": "@design-platform/wx-sdk/index"
},
"componentPlaceholder": {
"pay-payment": "view",
"browse-log-collector": "view",
"design-sdk": "view"
}
}
onPageScroll({ scrollTop }) {
this.setData({
isShowDesc: true
});
}
const queryDom = wx.createSelectorQuery().in(this);
const queryGoodsDes = queryDom.select('#detail');
if (queryGoodsDes) {
queryGoodsDes
.boundingClientRect((rect) => {
if (!rect) return;
if ((wx.rprm as any)?.systemInfo.windowHeight - rect.top > rect.height) {
this.setData(
{
isShowDesc: true,
},
() => this.emitGoodsDetail(),
);
}
})
.exec();
}
this.setData({ parseHtml }, () => { // 此处做滚动到图片位置的时候加载图片 parseHtml.images.forEach((item, index) => { const { obList } = this.data; const idx = item.index.replace(/(\d+)\.?/g, (s, $1) => `nodes[${$1}].`); if (!this.data.imgLoayLoad) { this.setData({ [`parseHtml.${idx}imgShow`]: true, [`parseHtml.${idx}imgLoayLoad`]: this.data.imgLoayLoad, }); return; } if (obList[index]) { obList[index].disconnect(); } obList[index] = this.createIntersectionObserver().relativeToViewport({ bottom: 750 }); obList[index].observe(`.wx-parse-img${item.imgIndex}`, (res) => { const idx = item.index.replace(/(\d+)\.?/g, (s, $1) => `nodes[${$1}].`); if (res.intersectionRatio > 0) { obList[index].disconnect(); this.setData({ [`parseHtml.${idx}imgShow`]: true, [`parseHtml.${idx}imgLoayLoad`]: this.data.imgLoayLoad, }); } }); }); });
四、运行性能优化
const BulletChatConfig = {
// 消费弹幕配置
};
Component({
data: {
originalData: [],
},
bulletChatConfig:bulletChatConfig,
pageLifetimes: {
show() {
this.animateStart();
},
}
});
// before: ❌ 不要在setData中偷懒一次性传所有的data;
this.setData({
singleSkuData: {
...skuData,
selectedMap: this.data.selectedMap,
},
});
// after
this.setData({
['singleSkuData.selectedMap']: this.data.selectedMap,
});
const that = this;
const timer = setInterval(function () {
if (dataIndex === data.length) {
dataIndex = 0;
}
if (queueIndex === rowQueue.length) {
queueIndex = 0;
}
tempData.push(that.getBulletChatItem(data[dataIndex], rowQueue[queueIndex], config.itemStyle));
if (tempData.length > 8) {
tempData.splice(0, 1);
}
that.setData({
bulletChatList: tempData,
});
dataIndex++;
queueIndex++;
}, config.intervalTime);
that.setData({ timer, type: customType || DisplayType[display] || 'scrollup', bulletChatConfig: config });
this.intersectionObserver = wx.createIntersectionObserver(this, { observeAll: true, thresholds: [0, 0.5, 1, 0] });
if (!this.intersectionObserver) return;
this.intersectionObserver.relativeTo('.bullet-chat').observe('.bullet-chat-item-container', (res) => {
if (res.intersectionRatio >= 0.5) {
let {
dataset: { id },
} = res;
if (id === data.length - 1) {
id = -1;
}
this.setData({
[`bulletChatList[${id + 1}].style`]: config.itemStyle,
});
}
if (res.intersectionRatio === 0 && res.intersectionRect.width === 0) {
const {
dataset: { id },
} = res;
this.setData({
[`bulletChatList[${id}].style`]: 'left: 100%',
});
}
})
五、运优化结果展示
六、总结