Skip to content

Commit

Permalink
perf(Cast): memoize/cache canDisplayType results to reduce startup la…
Browse files Browse the repository at this point in the history
…tency (#6367)

Related to
#5776 (comment)
  • Loading branch information
avelad committed Mar 26, 2024
1 parent d0aa697 commit 30285b2
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/polyfill/media_capabilities.js
Expand Up @@ -300,7 +300,17 @@ shaka.polyfill.MediaCapabilities = class {
// this query is specifically for HDR.
displayType += '; eotf=smpte2084';
}
return cast.__platform__.canDisplayType(displayType);
let result = false;
if (displayType in shaka.polyfill.MediaCapabilities
.memoizedCanDisplayTypeRequests_) {
result = shaka.polyfill.MediaCapabilities
.memoizedCanDisplayTypeRequests_[displayType];
} else {
result = cast.__platform__.canDisplayType(displayType);
shaka.polyfill.MediaCapabilities
.memoizedCanDisplayTypeRequests_[displayType] = result;
}
return result;
}

/**
Expand Down Expand Up @@ -337,6 +347,15 @@ shaka.polyfill.MediaCapabilities.originalMcap = null;
*/
shaka.polyfill.MediaCapabilities.memoizedMediaKeySystemAccessRequests_ = {};

/**
* A cache that stores the canDisplayType result of calling
* `cast.__platform__.canDisplayType`.
*
* @type {(Object<(!string), (!boolean)>)}
* @export
*/
shaka.polyfill.MediaCapabilities.memoizedCanDisplayTypeRequests_ = {};

// Install at a lower priority than MediaSource polyfill, so that we have
// MediaSource available first.
shaka.polyfill.register(shaka.polyfill.MediaCapabilities.install, -1);

0 comments on commit 30285b2

Please sign in to comment.