UNPKG

console-performance

Version:

Simple performance.now shim with automatic logging

95 lines (66 loc) 2.12 kB
(function () { 'use strict'; /** Shim from https://gist.github.com/davidwaterston/2982531 */ var performanceNow = (function() { var performance = (typeof window !== 'undefined' && window.performance) ? window.performance : {}; performance.now = (function() { return performance.now || performance.webkitNow || performance.msNow || performance.oNow || performance.mozNow || function() { return new Date().getTime(); }; })(); return performance.now(); }); var consolePerformance = { _tests: [], start: function (tag, text, clean) { if ((typeof DEBUG !== 'undefined' && DEBUG) || (typeof process === 'object' && process.env.DEBUG)) { this._tests[tag] = performanceNow(); if (!text) { text = tag; } if (clean) { return console.log(text, tag); } if (typeof window !== 'undefined') { return console.log('%c' + text, 'color: #27ae60', tag); } console.log('\x1b[32m%s\x1b[0m ', text, tag); } }, end: function (tag, text, clean) { if ((typeof DEBUG !== 'undefined' && DEBUG) || (typeof process === 'object' && process.env.DEBUG)) { if (!this._tests.hasOwnProperty(tag)) { return false; } var now = performanceNow(), test = this._tests[tag], diff = now - test, logText; delete this._tests[tag]; if (!text) { logText = tag; } else { logText = text; } if (clean) { return console.log(logText, tag, diff); } if (typeof window !== 'undefined') { if (text) { return console.log('%c' + logText, 'color: #c0392b', tag, diff); } return console.log('%c' + logText, 'color: #c0392b', tag, diff); } if (text) { return console.log('\x1b[31m%s\x1b[0m ', logText, tag, diff); } return console.log('\x1b[31m%s\x1b[0m ', logText, tag, diff); } } }; if (typeof module !== 'undefined' && module.exports) { module.exports = consolePerformance; } else if (typeof this !== 'undefined') { this.consolePerformance = consolePerformance; } }).call(this);