0.0.4 • Published 5 years ago
@saber2pr/timer v0.0.4
@saber2pr/timer
async time counter
npm install @saber2pr/timer
git clone https://github.com/Saber2pr/saber-timer.git
API
Timer
name
任务名task
异步函数
Timer(name, task)
Timers
name
任务名task
异步函数- 返回
then链
Examples
- Timer
Timer('quickSort', async () => sortQuick([2, 1, 5, 4])).then(console.log) // quickSort: 1.446ms [ 1, 2, 4, 5 ]
Timer('bubbleSort', async () => sortB([2, 1, 5, 4])).then(console.log) // bubbleSort: 3.502ms [ 1, 2, 4, 5 ]
Timer(
'timeout',
() => new Promise(resolve => setTimeout(() => resolve(), 1000))
) // timeout: 1004.032ms
Timer(
'quickSort',
() =>
new Promise(resolve =>
setTimeout(() => resolve(sortQuick([2, 1, 5, 4])), 1000)
)
).then(console.log)
Timer('bubbleSort', async () => sortB([2, 1, 5, 4])).then(console.log)
// bubbleSort: 1.982ms
// [ 1, 2, 4, 5 ]
// quickSort: 1005.779ms
// [ 1, 2, 4, 5 ]
Promise.all([
Timer(
'quickSort',
() =>
new Promise(resolve =>
setTimeout(() => resolve(sortQuick([2, 1, 5, 4])), 1000)
)
),
Timer('bubbleSort', async () => sortB([2, 1, 5, 4]))
]).then(res => console.log(...res))
// bubbleSort: 2.003ms
// quickSort: 1004.435ms
// [ 1, 2, 4, 5 ] [ 1, 2, 4, 5 ]
- Timers
const delay = (time: number) =>
new Promise(res => setTimeout(() => res(), time))
Timers('timeout0', async () => await delay(2000))
.then('timeout1', async () => await delay(1000))
.then('timeout2', async () => await delay(500))
// timeout2: 505.732ms
// timeout1: 1000.622ms
// timeout0: 2002.914ms
start
npm install
npm start
npm test
Author: saber2pr
develope and test
you should write ts in /src
you should make test in /src/test
export your core in /src/index.ts!