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

JavaScript performance comparison

Revision 98 of this test case created

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() {
    style.WebkitTransform = "none";
    style.MozTransform = "none";
    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 translate = 'translate3d(' + x + 'px, ' + y + 'px, 0px)';
  style.WebkitTransform = translate;
  style.MozTransform = translate;
  style.OTransform = translate;
  style.transform = translate;
  elem._width = elem.offsetWidth;
}
pending…
translateX/Y
var x = y = 0;
for (var i = 100; i--;) {
  x = i;
  y = i * 5;
  var translate = 'translateX(' + x + 'px) translateY(' + y + 'px)';
  style.WebkitTransform = translate;
  style.MozTransform = translate;
  style.OTransform = translate;
  style.transform = translate;
  elem._width = elem.offsetWidth;
}
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';
  elem._width = elem.offsetWidth;
}
pending…
translate2d
var x = y = 0;
for (var i = 100; i--;) {
  x = i;
  y = i * 5;
  var transform = 'translate(' + x + 'px, ' + y + 'px)';
  style.WebkitTransform = transform;
  style.MozTransform = transform;
  style.OTransform = transform;
  style.transform = transform;
  elem._width = elem.offsetWidth;
}
pending…
scroll
var x = y = 0;
for (var i = 100; i--;) {
  x = i;
  y = i * 5;
  translator.scrollLeft = x;
  translator.scrollTop = y;
}
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