aurelia-pal-browser
Version:
The browser-specific implementation of Aurelia's platform abstraction layer.
109 lines (85 loc) • 2.83 kB
JavaScript
import { _PLATFORM } from './platform';
if (typeof FEATURE_NO_IE === 'undefined') {
// performance polyfill. Copied from https://gist.github.com/paulirish/5438650
// https://gist.github.com/paulirish/5438650
// @license http://opensource.org/licenses/MIT
// copyright Paul Irish 2015
if ('performance' in window === false) {
window.performance = {};
}
if ('now' in window.performance === false) {
let nowOffset = Date.now();
if (performance.timing && performance.timing.navigationStart) {
nowOffset = performance.timing.navigationStart;
}
window.performance.now = function now() {
return Date.now() - nowOffset;
};
}
const startOffset = Date.now ? Date.now() : + (new Date);
const _entries = [];
const _marksIndex = {};
function _filterEntries(key, value) {
var i = 0, n = _entries.length, result = [];
for (; i < n; i++) {
if (_entries[i][key] == value) {
result.push(_entries[i]);
}
}
return result;
}
function _clearEntries(type, name) {
var i = _entries.length, entry;
while (i--) {
entry = _entries[i];
if (entry.entryType == type && (name === void 0 || entry.name == name)) {
_entries.splice(i, 1);
}
}
};
if (!window.performance.mark) {
window.performance.mark = window.performance.webkitMark || function (name) {
const mark = {
name,
entryType: "mark",
startTime: window.performance.now(),
duration: 0
};
_entries.push(mark);
_marksIndex[name] = mark;
};
}
if (!window.performance.measure) {
window.performance.measure = window.performance.webkitMeasure || function (name, startMark, endMark) {
startMark = _marksIndex[startMark].startTime;
endMark = _marksIndex[endMark].startTime;
_entries.push({
name,
entryType: "measure",
startTime: startMark,
duration: endMark - startMark
});
};
}
if (!window.performance.getEntriesByType) {
window.performance.getEntriesByType = window.performance.webkitGetEntriesByType || function (type) {
return _filterEntries("entryType", type);
};
}
if (!window.performance.getEntriesByName) {
window.performance.getEntriesByName = window.performance.webkitGetEntriesByName || function (name) {
return _filterEntries("name", name);
};
}
if (!window.performance.clearMarks) {
window.performance.clearMarks = window.performance.webkitClearMarks || function (name) {
_clearEntries("mark", name);
};
}
if (!window.performance.clearMeasures) {
window.performance.clearMeasures = window.performance.webkitClearMeasures || function (name) {
_clearEntries("measure", name);
};
}
_PLATFORM.performance = window.performance;
}