Created
May 27, 2017 05:34
-
-
Save ccnokes/bcb621e4a6b04d62d842b6cbc06a8cf6 to your computer and use it in GitHub Desktop.
script for detecting DOM method calls that are know to cause a reflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// These methods and getter/setters force layout/reflow in Chrome/WebKit | |
// From https://gist.github.com/paulirish/5d52fb081b3570c81e3a | |
const getterSetters = [ | |
'offsetLeft', | |
'offsetTop', | |
'offsetWidth', | |
'offsetHeight', | |
'offsetParent', | |
'clientLeft', | |
'clientTop', | |
'clientWidth', | |
'clientHeight', | |
]; | |
const methods = [ | |
'getClientRects', | |
'getBoundingClientRect', | |
]; | |
function logger(methodName, el) { | |
console.log(`${methodName} called on: `, el.nodeName + '#' + el.classList.toString()); | |
} | |
//create a separate JS context that's clean | |
const iframe = document.createElement('iframe'); | |
//have to append it to get access to HTMLElement | |
document.body.appendChild(iframe); | |
getterSetters.forEach(method => { | |
//grab the setter. note that __lookupSetter__ is deprecated maybe try getOwnPropertyDescriptor? anyways this works currently | |
let origGetter = iframe.contentWindow.HTMLElement.prototype.__lookupGetter__(method); | |
let origSetter = iframe.contentWindow.HTMLElement.prototype.__lookupSetter__(method); | |
//mangle the global HTMLElement in this JS context | |
Object.defineProperty(HTMLElement.prototype, method, { | |
set: function (val) { | |
logger('set ' + method, this); | |
return origSetter.call(this, val); //allow the method to be called like normal | |
}, | |
get: function() { | |
logger('get ' + method, this); | |
return origGetter.call(this); //allow the method to be called like normal | |
} | |
}); | |
}); | |
methods.forEach(method => { | |
const origFn = HTMLElement.prototype[method]; | |
HTMLElement.prototype[method] = function() { | |
logger(method, this); | |
return origFn.apply(this, arguments); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment