npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wx-performance

v1.0.1

Published

微信小程序测速耗时计算SDK

Downloads

1

Readme

wx-performance

微信小程序测速耗时管理SDK

使用此组件需要依赖小程序基础库 2.2.1 以上版本,同时依赖开发者工具的 npm 构建。具体详情可查阅官方 npm 文档

说明

简化耗时管理和上报逻辑

小程序·测速系统开始内测。测速系统旨在帮助开发者简单方便的测速网络性能、渲染/加载性能等。 测速结果数据支持地域、运营商、系统、网络类型等关键维度交叉分析,支持分钟级数据实时查看。

微信只提供了wx.reportPerformance(id, cost)接口,具体的耗时计算需要业务方自己计算。一般情况下我们计算任务耗时都是通过打点的方式,比如:

let t1 = new Date().getTime();
let pfmId = 1001;

// some sync task 
...

let cost = new Date().getTime() - t1;
if (wx.reportPerformance) {
	wx.reportPerformance(pfmId, cost);
}


// ============

let t2 = new Date().getTime();
let pfmId1 = 1002;

// some async task
setTimeout(() => {
	let cost = new Date().getTime() - t2;
	if (wx.reportPerformance) {
		wx.reportPerformance(pfmId1, cost);
	}
}, 100)

我们可以看到常规打点的方式代码比较繁琐:

  1. 需要自己进行耗时计算
  2. 变量多,增加维护成本
  3. 接口兼容性处理

本SDK旨在提供一种更简单的方式进行耗时管理和上报,使用SDK后的代码:

var Performance = require('wx-performance')
let pfmId = 1001;
let pfmId1 = 1002;

let record = Performance.startRecord(pfmId);
// some sync task 
...
record.finish();

// ============

let record1 = Performance.startRecord(pfmId1);

// some async task
setTimeout(() => {
	record1.finish();
}, 100)

Performance.startRecord接口返回一个有finish接口的对象,只需要在结束的地方执行就会进行耗时自动计算与上报。

防重复调用处理

有时我们会出现重复调用finish接口的场景,导致上报数据不准确,比如:我们需要计算页面第一次onShow的耗时,我们的代码可能如下:

let pfmId = 1001;

Page({
	t1: null,
	
	onLoad() {
		this.t1 = new Date().getTime();
	},
	
	onShow() {
		let cost = new Date().getTime() - this.t1;
		wx.reportPerformance(pfmId, cost);
	}
})

而我们都知道onShow会被多次调用,比如从下一级页面返回当前页,就会导致上报异常数据。为了避免这种情况,finish接口做了防重复调用的处理。

var Performance = require('wx-performance')
let pfmId = 1001;

Page({
	record: null,
	
	onLoad() {
		this.record = Performance.startRecord(pfmId);
	},
	
	onShow() {
    // finish接口只会上传第一次掉用的数据
		this.record.finish()
	}
})

安装

npm install --save wx-performance

使用

var Performance = require('wx-performance')
let pfmId = 1001;

let record = Performance.startRecord(pfmId)
// some sync task 
...
record.finish()