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

JavaScript performance comparison

Revision 93 of this test case created

Info

All cases with vendor prefixes

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

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
translate3d
var x = y = 0;
for (var i = 100; i--;) {
  x = i;
  y = i * 5;
  var transform = 'translate3d(' + x + 'px, ' + y + 'px, 0px)';
  if (style.WebkitTransform) {
    style.WebkitTransform = transform;
  } else if (style.MozTransform) {
    style.MozTransform = transform;
  } else {
    style.OTransform = transform;
  }
}
pending…
translateX/Y
var x = y = 0;
for (var i = 100; i--;) {
  x = i;
  y = i * 5;
  var transform = 'translateX(' + x + 'px) translateY(' + y + 'px)';
  if (style.WebkitTransform) {
    style.WebkitTransform = transform;
  } else if (style.MozTransform) {
    style.MozTransform = transform;
  } else {
    style.OTransform = transform;
  }
}
pending…
css left/top
var x = y = 0;
for (var i = 100; i--;) {
  x = i;
  y = i * 5;
  style.left = x + 'px';
  style.top = y + 'px';
}
pending…
translate2d
var x = y = 0;
for (var i = 100; i--;) {
  x = i;
  y = i * 5;
  var transform = 'translate(' + x + 'px, ' + y + 'px)';
  if (style.WebkitTransform) {
    style.WebkitTransform = transform;
  } else if (style.MozTransform) {
    style.MozTransform = transform;
  } else {
    style.OTransform = transform;
  }
}
pending…
scrollTop/scrollLeft
var x = y = 0;
for (var i = 100; i--;) {
  x = i;
  y = i * 5;
  translator.scrollLeft = x;
  translator.scrollTop = y;
}
pending…
translate
var x = y = 0;
for (var i = 100; i--;) {
  x = i;
  y = i * 5;
  var transform = 'translate(' + x + 'px, ' + y + 'px)';
  if (style.WebkitTransform) {
    style.WebkitTransform = transform;
  } else if (style.MozTransform) {
    style.MozTransform = transform;
  } else {
    style.OTransform = transform;
  }
}
pending…
matrix3d
var x = 0,
    y = 0;
for (var i = 100; i--;) {
  x = i;
  y = i * 5;
  var transform = 'matrix3d(1,0,0,0,0,1,0,0,0,0,1,0,' + x + ',' + y + ',0,1)';
  if (style.WebkitTransform) {
    style.WebkitTransform = transform;
  } else if (style.MozTransform) {
    style.MozTransform = transform;
  } else {
    style.OTransform = transform;
  }
}
pending…
matrix
var x = 0,
    y = 0;
for (var i = 100; i--;) {
  x = i;
  y = i * 5;
  var transform = 'matrix(1,0,0,1,' + x + ',' + y + ',0)';
  if (style.WebkitTransform) {
    style.WebkitTransform = transform;
  } else if (style.MozTransform) {
    style.MozTransform = transform;
  } else {
    style.OTransform = transform;
  }
}
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