D3js SVG vs DIV

JavaScript performance comparison

Revision 2 of this test case created by Raul Fernandez

Info

Test performance using D3js library and building graphics using SVG elements (G, RECT) or DOM elements (DIVS)

Preparation code

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

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//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

Test runner

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

Java applet disabled.

Testing in unknown unknown
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. Here’s a list of current revisions for this page:

0 comments

Add a comment