Calculate offset to body (offsetTop) (v2)

Revision 2 of this benchmark created on


Preparation HTML

<div style="height:500px;"></div>
<div id="getMyOffset"></div>

Test runner

Ready to run.

Testing in
TestOps/sec
For loop
var result = function (element) {
    if(typeof element.offsetParent !== 'undefined') {
        var posY = 0;
        for (posY; element; element = element.offsetParent) {
            posY += element.offsetTop;
        }
        return posY;
    } else {
        return element.y;
    }
}(document.getElementById('getMyOffset'));
ready
Do while
var result = function (element) {
    if(typeof element.offsetParent !== 'undefined') {
        var curtop = 0;
        do {
            curtop += element.offsetTop;
            element = element.offsetParent
        } while (element);
        
        return curtop;
    } else {
        return element.y;
    }
}(document.getElementById('getMyOffset'));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.