-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad5dcb5
commit 732628f
Showing
5 changed files
with
66 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict' | ||
|
||
const PerformanceNow = | ||
(global.performance && global.performance.now) || | ||
global.performanceNow || | ||
global.nativePerformanceNow || (() => { try { | ||
var now = require('fbjs/lib/performanceNow') | ||
} finally { return now }})(); | ||
|
||
const DEFAULT_LABEL = 'default'; | ||
const DEFAULT_PREC = 3; | ||
|
||
let counts = {}; | ||
let startTimes = {}; | ||
|
||
const fixed = n => Math.trunc(n) === n ? n + '' : n.toFixed(DEFAULT_PREC); | ||
|
||
console.time = console.time || ((label = DEFAULT_LABEL) => { startTimes[label] = PerformanceNow() }); | ||
console.timeLog = console.timeLog || ((label = DEFAULT_LABEL, desc) => timeRecord(label, desc)); | ||
console.timeEnd = console.timeEnd || ((label = DEFAULT_LABEL) => timeRecord(label, undefined, true)); | ||
|
||
console.count = console.count || ((label = DEFAULT_LABEL) => { | ||
if (!counts[label]) { | ||
counts[label] = 0; | ||
} | ||
counts[label]++; | ||
console.log(`${label}: ${counts[label]}`); | ||
}); | ||
|
||
console.countReset = console.countReset || ((label = DEFAULT_LABEL) => { | ||
if (counts[label]) { | ||
counts[label] = 0; | ||
} else { | ||
console.warn(`Count for '${label}' does not exist`); | ||
} | ||
}); | ||
|
||
function timeRecord(label, desc, final) { | ||
const endTime = PerformanceNow(); | ||
const startTime = startTimes[label]; | ||
if (startTime) { | ||
const delta = endTime - startTime; | ||
if (desc) { | ||
console.log(`${label}: ${fixed(delta)}ms ${desc}`); | ||
} else { | ||
console.log(`${label}: ${fixed(delta)}ms`); | ||
} | ||
if (final) delete startTimes[label]; | ||
} else { | ||
console.warn(`Timer '${label}' does not exist`); | ||
} | ||
} |