UNPKG

903 BJavaScriptView Raw
1var assert = require('assert')
2
3var hasPerf = typeof window !== 'undefined' &&
4 window.performance && window.performance.mark
5
6module.exports = nanotiming
7
8function nanotiming (name) {
9 assert.equal(typeof name, 'string', 'nanotiming: name should be type string')
10
11 if (!hasPerf) return noop
12
13 var uuid = (window.performance.now() * 100).toFixed()
14 var startName = name + '-start-' + uuid
15 window.performance.mark(startName)
16
17 return function () {
18 var endName = name + '-end-' + uuid
19 window.performance.mark(endName)
20
21 ric(function () {
22 var measureName = name + ' [' + uuid + ']'
23 window.performance.measure(measureName, startName, endName)
24 window.performance.clearMarks(startName)
25 window.performance.clearMarks(endName)
26 })
27 }
28}
29
30function ric (cb) {
31 if (this.hasIdleCallback) window.requestIdleCallback(cb)
32 else setTimeout(cb, 0)
33}
34
35function noop () {}