documentOffsetTop vs offset().top
JavaScript performance comparison
Preparation code
<div id="static" class="offset">
0
</div>
<div id="static2" class="offset">
0
</div>
<div id="static-wrapped-static">
<br />
<div class="offset">
0
</div>
</div>
<div id="absolute" class="offset">
0
</div>
<div id="relative" class="offset">
0
</div>
<div id="fixed-side" class="offset">
0
</div>
<div id="fixed-top" class="offset">
0
</div>
<div id="fixed-bottom" class="offset">
0
</div>
<div style="position: relative;">
<div id="relative-wrapped-absolute">
<div class="offset">
0
</div>
</div>
</div>
<style>
body {padding-left: 12px;} #absolute { top: 100px; position: absolute; } #relative
{ top: 150px; position: relative; } #fixed-side { right: 0; position: fixed; } #fixed-top
{ left: 50%; position: fixed; top: 0; } #fixed-bottom { left: 50%; position: fixed;
bottom: 0; } #relative-wrapped-absolute { top: 8px; position: relative; } #relative-wrapped-absolute
div { position: absolute; top: 20px; } .offset { border: 1px solid black; } #grid
{ height: 100%; left: 0; position: absolute; top: 1px; width: 100%; z-index: -1;
} .grid-line { border-bottom: 1px solid lightgray; font-size: 8px; height: 9px; line-height:
20px; }
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
Element.prototype.documentOffsetTop = function() {
return this.offsetTop + (this.offsetParent ? this.offsetParent.documentOffsetTop : 0);
};
};
</script>
Preparation code output
0
0
0
0
0
0
0
0
0
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
documentOffsetTop |
|
pending… |
offset().top |
|
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:
- Revision 1: published by ThinkingStiff
- Revision 2: published by Renan
0 comments