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

just-stopwatch

v1.3.1

Published

just a stopwatch

Downloads

21

Readme

just-stopwatch

可用于测量代码运行时间

Measuring code execution time

测量得出的时间 = 测量代码时间 + 目标代码运行时间

measure time = stopwatch execution time + target code execution time

测量代码时间 可能约为 1ms ~ 3ms

stopwatch execution time cost about 1ms ~ 3ms

[x] Node.js > 8.0 (maybe support the older version)

[x] Browser

Install

npm i just-stopwatch

Usage

const stopwatch = require('just-stopwatch')

const doSth = (ms=1e3)=> {
  Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
}

// 简单地测量一下代码运行耗时
// simply measure `doSth()` execution time
const sw = stopwatch().Start()
doSth(3e3)
const cost = sw.Stop()
console.log(cost, 'ms') // 3000 ms

// 多次获取间隔时间
// multi slice
const sw = stopwatch().Start()
doSth()
console.log(sw.Slice()) // Start 是最早的一个 slice
doSth(2e3)
console.log(sw.Slice()) // 距离上一个 Slice() 已经过去了多少时间
console.log(sw.Stop(), 'ms') // 3000 ms

// 多次获取计时结果
const sw = stopwatch().Start()
doSth() // 1e3 ms
console.log(sw.Split()) // 1000
doSth(2e3)
console.log(sw.Split()) // 3000
console.log(sw.Stop(), 'ms') // 3000 ms

// 暂停后继续
// pause and continue
const sw = stopwatch().Start()
doSth()
console.log(sw.Pause()) // 暂停测量
doSth(2e3) // be ignored
console.log(sw.Continue()) // 继续测量
doSth()  
console.log(sw.Stop(), 'ms') // 三次 doSth(),但第二次没有测量

// 倒计时
// Countdown
const sw = stopwatch()
sw.On(`Countdown/timeout`, ({ ms}) => {
  console.log(ms,'实际超时时间,必然大于设定时间')
})
.Countdown(3e3) // 设置了 3 秒,则 timeout 回调时间 必然 大于 设定时间

// 倒计时重启
const sw = stopwatch().On('Countdown/timeout', ({ ms }) => {
   console.log(ms) // 2e3 因为倒计时被重新设定
})
sw.Countdown(4e3) // 设定 4 秒倒计时
sw.CountdownRestart(2e3) // 立即将倒计时设定为 2 秒

// 倒计时暂停后继续

const timeout = 5e3
const sw = stopwatch()
sw.On('Countdown/timeout', ({ ms }) => {
  console.log('倒计时实际超时时间',ms)
})
sw.Countdown(timeout) // 5s
doSth(1e3) 
sw.CountdownPause() // 经过 1s 后暂停
doSth(1e3) 
sw.CountdownContinue() // 经过 1s 后继续
sw.CountdownPause() // 立即暂停
doSth(1e3) 
sw.CountdownContinue() // 经过一秒后继续

Methods

计时方法

Start()

开始

Returns Stopwatch

Stop()

停止

Returns number 开始至停止的时间

Pause()

暂停

Returns number 开始至暂停的时间

Continue()

继续

Returns number 开始至暂停的时间

Split()

立即获取当前计时

Returns number 开始至现在的时间

Slice()

获取本次分割的时间

Returns number 距离上次 Slice() 的时间。Start() 为最早的一次 Slice()

Restart()

重置计时

Returns Stopwatch

倒计时

Countdown(ms)

开始

ms:number 倒计时间,毫秒

Returns Stopwatch

CountdownPause()

暂停

Returns number 剩余时间

CountdownContinue()

继续

Returns number 剩余时间

CountdownRemain()

获取剩余时间

Returns number 剩余时间

CountdownRestart(ms)

倒计时重置。可以中断已有的倒计时

ms:number 倒计时间,毫秒

Returns Stopwatch

事件监听

使用 On(event,callback) 进行事件监听 | event | callback args | 触发条件 | 回调参数解释 | | ----------------- | ------------- | ---------------- | --------------- | | Start | null | 计时开始时触发 | | | Stop | {ms:number} | 计时结束时触发 | ms:毫秒 | | Countdown/timeout | {ms:number} | 倒计时超时时触发 | ms:实际超时时间 |

Test

npm run test