D3js SVG vs DIV

JavaScript performance comparison

Revision 4 of this test case created by Raul Fernandez

Preparation code

<div id="divContainer"></div>
<svg id="svgContainer"></svg>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
      
<script>
Benchmark.prototype.setup = function() {
  function dataSourceGenerator(size, maxRange) {
    var i, data, obj, round, random;
  
    i = 0;
    data = [];
    round = Math.round;
    random = Math.random;
  
    for (i; i < size; i += 1) {
      obj = {};
  
      obj.x = round(maxRange * random());
      obj.y = round(maxRange * random());
      obj.width = round(maxRange * random());
      obj.height = round(maxRange * random());
  
      data.push(obj);
    }
  
    return data;
  }
  
  var dataSource = dataSourceGenerator(1000, 100);
  
  var divContainer = d3.select("#divContainer");
  var svgContainer = d3.select("#svgContainer");

};

Benchmark.prototype.teardown = function() {
  divContainer.data([]).exit().remove();
  svgContainer.data([]).exit().remove();

};
</script>

Preparation code output

<div id="divContainer"></div> <svg id="svgContainer"></svg> <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>

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
SVG
var columns = svgContainer.selectAll("rect").data(dataSource);

columns.enter()
  .append("rect")
  .each(function(d) {
    this.width = d.width;
    this.height = d.height;
    this.x = d.x;
    this.y = d.y;
  });
pending…
DIV
var columns = divContainer.selectAll("div").data(dataSource);

columns.enter()
  .append("div")
  .each(function(d) {
    var style = this.style;
    style.width = d.width + "px";
    style.height = d.height + "px";
    style.left = d.x + "px";
    style.top = d.y + "px";
  });
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