Follow up for #1815602: Introduce a polyfill for matchMedia

Problem/Motivation

The matchmedia polyfill is slow when there're a lot of pictures, because for each evaluation of a media query and new div is build to evaluate the media query.

Proposed resolution

Add caching to the polyfill.

Remaining tasks

We added this to picture for Drupal 7 (commit) and did some profiling, the same has to be done for matchmedia.js
Code can be tested at http://picturefill7.h011.attiks.com/pictures

User interface changes

None

API changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

attiks’s picture

Issue tags: +Performance
Lukas von Blarer’s picture

Component: javascript » picture.module

You created a patch for the 7.x module. Would you port it? Should I?

attiks’s picture

Component: picture.module » javascript

#2You may do it if you want

PS: Matchmedia is not part of picture in D8, it's used by other parts as well, so moving back to javascript.

Lukas von Blarer’s picture

Status: Active » Needs review
FileSize
1.76 KB

Here are the changes made to picture 7.x ported to 8.x

Status: Needs review » Needs work

The last submitted patch, 1848500-matchmedia-caching-4.patch, failed testing.

attiks’s picture

+++ core/misc/matchmedia.jsundefined
@@ -26,6 +26,35 @@ window.matchMedia = window.matchMedia || (function (doc, window) {
+  ¶

trailing whitespace

PS: Try creating your patch using git diff

attiks’s picture

Assigned: attiks » Unassigned

Related: there's a 'pure' javascript implementation for matchmedia as well: https://github.com/weblinc/matchMedia which is a lot faster than native media queries implementation.

The Drupal 7 picture module supports both and the improvement is visible on slower (mobile) browsers.

Lukas von Blarer’s picture

How stable is this? Should the script be replaced entirely?

attiks’s picture

It is stable and works over well. The only thing that needs to be done is test all core functionality that depends on this.

Lukas von Blarer’s picture

Title: Add caching to matchmedia polyfill » Use performance optimized matchmedia polyfill
FileSize
6.47 KB

Attaching a patch and changing title. I did not test the patch until now. What depends on matchmedia?

Lukas von Blarer’s picture

Status: Needs work » Needs review
attiks’s picture

There's a new faster - js only - implementation available at https://github.com/weblinc/media-match, might be a good alternative as well

sun’s picture

Issue summary: View changes

Note that the divergence of this library from upstream came up in:

#2203431: [meta] Various asset (JavaScript) libraries have to be updated to a (minified) stable release prior to 8.0.0

It would be great if we could either contribute our changes to the original upstream library so that we can replace our fork with the upstream version, or replace our current fork with a completely new/different library, as seemingly proposed in recent comments here.

droplet’s picture

Status: Needs review » Closed (duplicate)
Related issues: +#2207629: Update matchMedia library to latest release

Some rewritten have been done in upstream. I'm closing this issue. welcome to reopen. Thanks :) #2207629: Update matchMedia library to latest release