Skip to content

Commit

Permalink
core(driver): save performance.now() to avoid conflict (#6387)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish authored Oct 25, 2018
1 parent 888bd6d commit 37a7db7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@
Object.defineProperty(document.getElementById("5934b"), 'matches', {
value: "blahblah"
});

// Ensure gatherers still work when the prototype is messed with
HTMLElement.prototype.matches = { value: "blahblah" };


// Ensure long-task collection still works when performance.now is redefined
window.performance.now = 'right now';
</script>

<div>
Expand Down
7 changes: 5 additions & 2 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1207,10 +1207,13 @@ class Driver {
* @return {Promise<void>}
*/
async cacheNatives() {
await this.evaluateScriptOnNewDocument(`window.__nativePromise = Promise;
await this.evaluateScriptOnNewDocument(`
window.__nativePromise = Promise;
window.__nativeError = Error;
window.__nativeURL = URL;
window.__ElementMatches = Element.prototype.matches;`);
window.__ElementMatches = Element.prototype.matches;
window.__perfNow = performance.now.bind(performance);
`);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions lighthouse-core/lib/page-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function wrapRuntimeEvalErrorInBrowser(err) {
*/
/* istanbul ignore next */
function registerPerformanceObserverInPage() {
window.____lastLongTask = window.performance.now();
window.____lastLongTask = window.__perfNow();
const observer = new window.PerformanceObserver(entryList => {
const entries = entryList.getEntries();
for (const entry of entries) {
Expand All @@ -64,12 +64,12 @@ function registerPerformanceObserverInPage() {
function checkTimeSinceLastLongTask() {
// Wait for a delta before returning so that we're sure the PerformanceObserver
// has had time to register the last longtask
return new Promise(resolve => {
const timeoutRequested = window.performance.now() + 50;
return new window.__nativePromise(resolve => {
const timeoutRequested = window.__perfNow() + 50;

setTimeout(() => {
// Double check that a long task hasn't happened since setTimeout
const timeoutFired = window.performance.now();
const timeoutFired = window.__perfNow();
const timeSinceLongTask = timeoutFired - timeoutRequested < 50 ?
timeoutFired - window.____lastLongTask : 0;
resolve(timeSinceLongTask);
Expand Down

0 comments on commit 37a7db7

Please sign in to comment.