getElementPositionTest3

JavaScript performance comparison

Test case created by Hedger

Info

Test what's the fastest way to get element's position.

Preparation code

<style>
  #test div { padding: 10px; margin: 10px; border: solid 1px red; position: relative; left:
  10px; top: 10px; } #foo { background: yellow; }
</style>
<div id="test">
<div>
  <div>
    <div>
      <div id="foo">
        text
      </div>
    </div>
  </div>
</div>
</div>

Preparation code output

text

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
testOffsetParent
var testOffsetParent = function(node) {
    var x = 0;
    var y = 0;
    do {
      x += node.offsetLeft;
      y += node.offsetTop;
      node = node.offsetParent;
    } while (node && node != document.body);
    return [x, y];
    };
var node = document.getElementById('foo');
testOffsetParent(node);
pending…
testBoundingClientRect
var testBoundingClientRect = function(node) {
    if (node.getBoundingClientRect) {
      testBoundingClientRect = function(node) {
        var box = node.getBoundingClientRect();
        return [box.left, box.top];
      };
    } else {
      testBoundingClientRect = function(node) {
        return [-1, -1];
      };
    }
    return testBoundingClientRect(node);
    };
var node = document.getElementById('foo');
testBoundingClientRect(node);
pending…
testWebkitConvertPointFromNodeToPage
var testWebkitConvertPointFromNodeToPage = function(node) {
    if (window.webkitConvertPointFromNodeToPage) {
      testWebkitConvertPointFromNodeToPage = function(node) {
        var point = new WebKitPoint(0, 0);
        point = webkitConvertPointFromNodeToPage(node, point);
        return [point.x, point.y];
      };
    } else {
      testWebkitConvertPointFromNodeToPage = function(node) {
        return [-1, -1];
      };
    }
    return testWebkitConvertPointFromNodeToPage(node);
    };




var node = document.getElementById('foo');
testWebkitConvertPointFromNodeToPage(node);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment