Skip to content

Commit

Permalink
feat(performance): update performance API
Browse files Browse the repository at this point in the history
this adds missing methods from the performance API specification. This is going to be needed for SSR.

Related issue: aurelia/store#19
  • Loading branch information
zewa666 committed Mar 6, 2018
1 parent 6b21d12 commit 1508a8b
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 4 deletions.
6 changes: 6 additions & 0 deletions spec/nodejs-pal-builder.spec.ts
Expand Up @@ -16,6 +16,12 @@ describe("pal-builder", () => {

it('should ensure performance', () => {
expect(pal.global.performance.now()).toBeGreaterThan(-1);
expect(pal.global.performance.measure).toBeDefined();
expect(pal.global.performance.mark).toBeDefined();
expect(pal.global.performance.getEntriesByType).toBeDefined();
expect(pal.global.performance.getEntriesByName).toBeDefined();
expect(pal.global.performance.clearMeasures).toBeDefined();
expect(pal.global.performance.clearMarks).toBeDefined();
});

it('should enusre mutation observer is loaded', () => {
Expand Down
85 changes: 81 additions & 4 deletions src/nodejs-pal-builder.ts
Expand Up @@ -83,21 +83,98 @@ function patchNotifyChange(window: Window) {


function ensurePerformance(window) {
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 === undefined) {
window.performance = {};
}

if (window.performance.now === undefined) {
let nowOffset = Date.now();

//if (performance.timing && performance.timing.navigationStart) {
// nowOffset = performance.timing.navigationStart;
//}

window.performance.now = function now() {
return Date.now() - nowOffset;
};
}

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);
};
}
}

function ensureMutationObserver(window) {
Expand Down
53 changes: 53 additions & 0 deletions src/performance.ts
Expand Up @@ -7,4 +7,57 @@ export interface IPerformance {
* @return The timestamp, measured in milliseconds, accurate to one thousandth of a millisecond.
*/
now(): number;

/**
* Removes the given mark from the browser's performance entry buffer.
*
* @param {string} [markName] A DOMString representing the name of the timestamp. If this argument is omitted, all performance entries with an entry type of "mark" will be removed.
* @memberof IPerformance
*/
clearMarks(markName?: string): void;

/**
* Removes the given measure from the browser's performance entry buffer.
*
* @param {string} [measureName] A DOMString representing the name of the timestamp. If this argument is omitted, all performance entries with an entry type of "measure" will be removed.
* @memberof IPerformance
*/
clearMeasures(measureName?: string): void;

/**
* Returns a list of PerformanceEntry objects based on the given name and entry type.
*
* @param {string} name The name of the entry to retrieve
* @param {string} [entryType] The type of entry to retrieve such as "mark". The valid entry types are listed in PerformanceEntry.entryType.
* @returns {*}
* @memberof IPerformance
*/
getEntriesByName(name: string, entryType?: string): any;

/**
* Returns a list of PerformanceEntry objects of the given entry type.
*
* @param {string} entryType The type of entry to retrieve such as "mark". The valid entry types are listed in PerformanceEntry.entryType.
* @returns {*}
* @memberof IPerformance
*/
getEntriesByType(entryType: string): any;

/**
* Creates a timestamp in the browser's performance entry buffer with the given name.
*
* @param {string} markName a DOMString representing the name of the mark
* @memberof IPerformance
*/
mark(markName: string): void;

/**
* Creates a named timestamp in the browser's performance entry buffer between two specified marks (known as the start mark and end mark, respectively).
*
* @param {string} measureName a DOMString representing the name of the measure.
* @param {string} [startMarkName] A DOMString representing the name of the measure's starting mark. May also be the name of a PerformanceTiming property.
* @param {string} [endMarkName] A DOMString representing the name of the measure's ending mark. May also be the name of a PerformanceTiming property.
* @memberof IPerformance
*/
measure(measureName: string, startMarkName?: string, endMarkName?: string): void;
}

0 comments on commit 1508a8b

Please sign in to comment.