translate3d vs translate2d vs translate XY vs css left/top vs scrollTop/scrollLeft

JavaScript performance comparison

Revision 143 of this test case created by Hang Ye

Preparation code

<div id="scrollTranslator" style="width:200px; height:200px; overflow: hidden"><div id="elem" style="position: relative; background:#ccc; width:1000px; height:1000px;"></div></div>
<script>
  var translator = document.getElementById("scrollTranslator");
  var elem = document.getElementById("elem");
  var style = elem.style;
</script>
      
<script>
Benchmark.prototype.teardown = function() {
    if (style.WebkitTransform) {
      style.WebkitTransform = "none";
    } else if (style.MozTransform) {
      style.MozTransform = "none";
    } else {
      style.OTransform = "none";
    }
    style.left = '0px';
    style.top = '0px';

};
</script>

Preparation code output

<div id="scrollTranslator" style="width:200px; height:200px; overflow: hidden"><div id="elem" style="position: relative; background:#ccc; width:1000px; height:1000px;"></div></div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
translateX/Y
function setScroll(i=0) {
  if (i === -500) return;
  setTimeout(() => {    
    style.transform = 'translateY(' + i + 'px)';
    setScroll(--i);
  },0);
}

setScroll();
pending…
scrollTop/scrollLeft
function setScroll(i=0) {
  if (i === 500) return;
  setTimeout(() => {
    translator.scrollTop = i;
    setScroll(++i);
  },0);
}

setScroll();
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.

0 Comments