Skip to content

Commit

Permalink
Merge pull request #10 from nicjansma/when-rt-exists
Browse files Browse the repository at this point in the history
v0.1.6: Better FF 35 support (or any browser that has PerformanceTimeline but not UT)
  • Loading branch information
nicjansma committed Feb 2, 2015
2 parents d2d15e5 + abb1e0c commit 44c5aee
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ Additional notes:
* v0.1.3 - 2014-08-07: Include dist/ dir in bower and npm packages
* v0.1.4 - 2014-10-28: Fix for Safari iOS 8
* v0.1.5 - 2015-01-12: Fix for FF 35
* v0.1.6 - 2015-02-01: Better FF 35 support (or any browser that has RT but not UT)
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "usertiming",
"version": "0.1.5",
"version": "0.1.6",
"homepage": "https://github.com/nicjansma/usertiming.js",
"authors": [
"Nic Jansma <nic@nicj.net>"
Expand Down
4 changes: 2 additions & 2 deletions dist/usertiming.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "W3C UserTiming polyfill",
"main": "./src/usertiming",
"author": "Nic Jansma",
"version": "0.1.5",
"version": "0.1.6",
"repository" : {
"type" : "git",
"url" : "http://github.com/nicjansma/usertiming.js.git"
Expand Down
60 changes: 55 additions & 5 deletions src/usertiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,22 @@
// whether or not the timeline will require sort on getEntries()
var performanceTimelineRequiresSort = false;

// whether or not ResourceTiming is natively supported but UserTiming is
// not (eg Firefox 35)
var hasNativeGetEntriesButNotUserTiming = false;

//
// If getEntries() and mark() aren't defined, we'll assume
// we have to shim at least some PT functions.
//
if (typeof(window.performance.getEntries) !== 'function' ||
typeof(window.performance.mark) !== 'function') {

if (typeof(window.performance.getEntries) === 'function' &&
typeof(window.performance.mark) !== 'function') {
hasNativeGetEntriesButNotUserTiming = true;
}

window.performance.userTimingJsPerformanceTimeline = true;

// copy prefixed version over if it exists
Expand Down Expand Up @@ -217,7 +227,9 @@
}
};

if (typeof(window.performance.getEntries) !== 'function') {
if (typeof(window.performance.getEntries) !== 'function' || hasNativeGetEntriesButNotUserTiming) {
var origGetEntries = window.performance.getEntries;

/**
* Gets all entries from the Performance Timeline.
* http://www.w3.org/TR/performance-timeline/#dom-performance-getentries
Expand All @@ -230,11 +242,26 @@
ensurePerformanceTimelineOrder();

// get a copy of all of our entries
return performanceTimeline.slice(0);
var entries = performanceTimeline.slice(0);

// if there was a native version of getEntries, add that
if (hasNativeGetEntriesButNotUserTiming && origGetEntries) {
// merge in native
Array.prototype.push.apply(entries, origGetEntries.call(window.performance));

// sort by startTime
entries.sort(function(a, b) {
return a.startTime - b.startTime;
});
}

return entries;
};
}

if (typeof(window.performance.getEntriesByType) !== 'function') {
if (typeof(window.performance.getEntriesByType) !== 'function' || hasNativeGetEntriesButNotUserTiming) {
var origGetEntriesByType = window.performance.getEntriesByType;

/**
* Gets all entries from the Performance Timeline of the specified type.
* http://www.w3.org/TR/performance-timeline/#dom-performance-getentriesbytype
Expand All @@ -249,6 +276,12 @@
// we only support marks/measures
if (typeof(entryType) === 'undefined' ||
(entryType !== 'mark' && entryType !== 'measure')) {

if (hasNativeGetEntriesButNotUserTiming && origGetEntriesByType) {
// native version exists, forward
return origGetEntriesByType.call(window.performance, entryType);
}

return [];
}

Expand All @@ -269,7 +302,9 @@
};
}

if (typeof(window.performance.getEntriesByName) !== 'function') {
if (typeof(window.performance.getEntriesByName) !== 'function' || hasNativeGetEntriesButNotUserTiming) {
var origGetEntriesByName = window.performance.getEntriesByName;

/**
* Gets all entries from the Performance Timeline of the specified
* name, and optionally, type.
Expand All @@ -284,6 +319,11 @@
*/
window.performance.getEntriesByName = function(name, entryType) {
if (entryType && entryType !== 'mark' && entryType !== 'measure') {
if (hasNativeGetEntriesButNotUserTiming && origGetEntriesByName) {
// native version exists, forward
return origGetEntriesByName.call(window.performance, name, entryType);
}

return [];
}

Expand All @@ -305,6 +345,16 @@
}
}

if (hasNativeGetEntriesButNotUserTiming && origGetEntriesByName) {
// merge in native
Array.prototype.push.apply(entries, origGetEntriesByName.call(window.performance, name, entryType));

// sort by startTime
entries.sort(function(a, b) {
return a.startTime - b.startTime;
});
}

return entries;
};
}
Expand Down Expand Up @@ -512,4 +562,4 @@
//
module.exports = window.performance;
}
}(typeof(window) !== 'undefined' ? window : undefined));
}(typeof(window) !== 'undefined' ? window : undefined));
10 changes: 5 additions & 5 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</head>
<body>
<h1 id="nodeunit-header">UserTiming.js Test Suite</h1>

<div>Added window.performance.now() support?
<span id='addedNow'></span>
<span id='addedNowPrefixed'></span>
Expand All @@ -28,18 +28,18 @@ <h1 id="nodeunit-header">UserTiming.js Test Suite</h1>
<span id='addedUserTiming'></span>
<span id='addedUserTimingPrefixed'></span>
</div>

<script>
// show what was added
document.getElementById('addedNow').innerHTML = window.performance.userTimingJsNow ? 'Yes' : 'No';
document.getElementById('addedNowPrefixed').innerHTML = window.performance.userTimingJsNowPrefixed ? '(prefixed)' : '';

document.getElementById('addedPerformanceTimeline').innerHTML = window.performance.userTimingJsPerformanceTimeline ? 'Yes' : 'No';
document.getElementById('addedPerformanceTimelinePrefixed').innerHTML = window.performance.userTimingJsPerformanceTimelinePrefixed ? '(prefixed)' : '';

document.getElementById('addedUserTiming').innerHTML = window.performance.userTimingJsUserTiming ? 'Yes' : 'No';
document.getElementById('addedUserTimingPrefixed').innerHTML = window.performance.userTimingJsUserTimingPrefixed ? '(prefixed)' : '';

// run tests
nodeunit.run({'test': this.exports});
</script>
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,4 @@

test.done();
};
})(exports);
})(exports);

0 comments on commit 44c5aee

Please sign in to comment.