translate vs position left/top vs css margin left/top

JavaScript performance comparison

Revision 56 of this test case created by dstereo

Preparation code

<div id="elem" style="position: absolute; background:#ccc; width:10px; height:10px;"></div>
<script>
  var elem = document.getElementById("elem");
  var style = elem.style;
</script>
<script>
Benchmark.prototype.setup = function() {
    var x = 0,
        t = '';
};
</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
translate
for (var i = 200; i--;) {
  x = i * 5;
  t = 'translate(' + x + 'px, ' + x + 'px)';
  style.WebkitTransform = t;
  style.MozTransform = t;
  style.OTransform = t;
  style.msTransform = t;
  style.transform = t;
}
pending…
translate X/Y
for (var i = 200; i--;) {
  x = i * 5;
  t = 'translateX(' + x + 'px) translateY(' + x + 'px)';
  style.WebkitTransform = t;
  style.MozTransform = t;
  style.OTransform = t;
  style.msTransform = t;
  style.transform = t;
}
pending…
position left/top
for (var i = 200; i--;) {
  x = i * 5;
  t = x + 'px';
  style.left = t;
  style.top = t;
}
pending…
margin left/top
for (var i = 200; i--;) {
  x = i * 5;
  t = x + 'px';
  style.marginLeft = t;
  style.marginTop = t;
}
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