eqcss / eqcss

EQCSS is a CSS Reprocessor that introduces Element Queries, Scoped CSS, a Parent selector, and responsive JavaScript to all browsers IE8 and up

Home Page:https://elementqueries.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Performance degradation when CSS is loaded using XMLHttpRequest

matthewreiter opened this issue · comments

EQCSS uses the following code to load CSS from link tags:

              var xhr = new XMLHttpRequest

              xhr.open('GET', script[i].src, true)
              xhr.send(null)
              xhr.onreadystatechange = function() {

                EQCSS.process(xhr.responseText)

              }

Because it doesn't verify that the ready state is done, it repeatedly processes partial responses, resulting in duplicate queries getting added to EQCSS.data. This has a worse impact for IE than other browsers because IE calls onreadystatechange every 4 KB of data downloaded, whereas Firefox and Chrome call onreadystatechange less frequently.

The MDN documentation (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange) includes an example on how to use onreadystatechange that checks the ready state (and for good measure also verifies that the request was successful):

xhr.onreadystatechange = function () {
  if(xhr.readyState === 4 && xhr.status === 200) {
    console.log(xhr.responseText);
  }
};

When I tried putting in the ready state check it fixed the performance problems I was seeing.

Thanks for reporting this! I haven't noticed this, is there a specific test case where you know you can observe this happening so I can take a peek?

Thank you as well for the suggested fix too! Once I can reproduce and test it shouldn't take long to get a patch out. I really appreciate you opening this issue <3

This would happen any time you have CSS referenced with a <link> tag and the linked CSS file is larger than 4 KB (the bigger it is, the worse the performance gets). I initially found the problem by putting a breakpoint in the browser debugger in the EQCSS.apply function and noticed that EQCSS.data had many duplicate entries.

You could probably reproduce the problem by making style.eqcss much larger and then loading link.html.

Thanks! I'll see if I can reproduce this :3

Thank you so much for researching & reporting this, I was able to send out a new v1.9.1 release just now including your suggested fix!

Let me know if this solves it for you, and hope you have a wonderful weekend <3

Thanks! I've confirmed that my problem is fixed in version 1.9.1.

Beautiful! Hope you have a wonderful week :3