-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
43 lines (35 loc) · 1.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import fbjsPerformanceNow from 'fbjs/lib/performanceNow'
const performanceNow = global.nativePerformanceNow || fbjsPerformanceNow
const bencherExcludes = {
'constructor':true,
}
const defaultOpts={excludes: bencherExcludes, log: console, verbose: false, threshold: 16}
function slowlog(that, exp=/.*/, opts={}){
if(!__DEV__){
return []
}
let bound = []
let componentName = that.constructor.displayName || that.constructor.name || 'Class'
const { threshold, log:c, verbose, excludes } = {...defaultOpts, ...opts}
c.log(`slowlog: DEV MODE SLOWLOG ENABLED FOR <${componentName}>`)
for (let name of Object.getOwnPropertyNames(Object.getPrototypeOf(that))) {
let f = that[name]
const benchkey = `${componentName}.${name}`
if(typeof f === 'function' && !excludes[name] && exp.test(name)){
that[name] = function(){
const start = performanceNow()
const res = f.apply(that, arguments)
const timing = performanceNow() - start
if(timing > threshold){
c.warn(`slowlog: ${benchkey} was slow at: ${timing.toFixed(3)}ms`)
}else if(verbose){
c.log(benchkey, timing.toFixed(6))
}
return res
}
bound.push(name)
}
}
return bound
}
export default slowlog