question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

window.getComputedStyle poor performance

See original GitHub issue

I am currently in the process of bringing in some code written for the browser to nodejs which involves running window.getComputedStyle on a lot of elements. In the browser we are seeing times under 500ms where as with jsdom’s implementation of getComputedStyle our times are above 5000ms.

I created a sample that takes ~3000ms to execture in nodejs using jsdom@9.4.1, jquery@3.1.0 and xmldom@0.1.22

var jsdom = require('jsdom');
var window = jsdom.jsdom().defaultView;
var DOMParser = require('xmldom').DOMParser;
var jQuery = $ = require('jquery')(window);

var xml = '<span style="color: #fff;">test</span>';
var xmlDom = $(xml);

console.time('getComputedStyle');
for (var i = 0; i < 1000; i++) {
    var cs = window.getComputedStyle(xmlDom[0]);
}
console.timeEnd('getComputedStyle');

And here is a jsfiddle to test in broswer which takes about ~0.5ms: https://jsfiddle.net/3dzLLf5x/

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
vsemozhetbytcommented, Jul 12, 2016

Node.js 6.3.0, jsdom 9.4.1, Win 7. Simplified test code:

process.stdout.write('Loading jsdom... ');
const jsdom = require('jsdom');
console.log('Loaded.\n');

const doc = jsdom.jsdom('');
const win = doc.defaultView;

console.time('getComputedStyle');
for (let i = 0; i < 1000; i++) {
    const cs = win.getComputedStyle(doc.body);
}
console.timeEnd('getComputedStyle');
getComputedStyle: 7193.454ms

In the last Chrome for about:blank:

console.time('getComputedStyle');
for (let i = 0; i < 1000; i++) {
    const cs = window.getComputedStyle(document.body);
}
console.timeEnd('getComputedStyle');
getComputedStyle: 3.725ms
1reaction
dperinicommented, May 21, 2017

@staramir / @vsemozhetbyt you can retry now using nwmatcher 1.4.0 the changes may be of help improving the performances. You will have to use both the VERBOSITY and LOGERRORS flags, setting them to false to mute both the exceptions and the console output.

Read more comments on GitHub >

github_iconTop Results From Across the Web

getComputedStyle local vs every time in function Performance
Is it better or worse to make the ComputedStyle a global variable? I strongly recommend avoiding actual global variables as much as possible....
Read more >
getComputedStyle performance 20x worse than Safari for ...
Compare to Safari (Windows) Workaround: If the table's display style is set to "none" for the duration of the layout, Firefox's performance difference...
Read more >
window.getComputedStyle performance - Prince forum
Although I doubt it's the cause of this performance issue, it looks like Prince is reloading the CSS files everytime the DOM reflows....
Read more >
JavaScript Window getComputedStyle() Method - W3Schools
The getComputedStyle() method gets the computed CSS properties and values of an HTML element. The getComputedStyle() method returns a CSSStyleDeclaration object ...
Read more >
How We Increased Our Plugin's Performance by 200%
What's more is that even when the getComputedStyle result was cached, it was still really slow. As it turns out, accessing the style...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found