Optimising DOM Access

JavaScript performance comparison

Test case created by Jon Howard

Preparation code

<div id="TargetDIV">Test DIV</div>
      
<script>
Benchmark.prototype.setup = function() {
  var targetDIV = document.getElementById("TargetDIV");
  var posX =0, prevPosX=0;

};
</script>

Preparation code output

<div id="TargetDIV">Test 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
Un-optimised. Move on every iteration.
posX+=0.2;
var roundX = Math.round(posX);

if(posX>500)posX=0;
targetDIV.style.left=roundX+"px";

prevPosX = roundX;
pending…
Optimised. Only move when necessary
posX+=0.2;
var roundX = Math.round(posX);
if(prevPosX!=roundX)
{
   if(posX>500)posX=0;
   targetDIV.style.left=roundX+"px";

   prevPosX = roundX;
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments

Test DIV