jquery plugin vs canvas sparklines

JavaScript performance comparison

Test case created by HelenFr

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="http://omnipotent.net/jquery.sparkline/2.1.2/jquery.sparkline.min.js">
</script>
<div class="tckr"><span id="sparkline"></span>12345</div>
      
<script>
Benchmark.prototype.setup = function() {
  /*  Rescale a number
      for x in range [x0, x1], rescale it to [y0, y1]
  */
  
  function rescale(x0, x1, y0, y1) {
    var a = (y1 - y0) / (x1 - x0);
    return function(x) {
      return (x - x0) * a + y0;
    }
  }
  /*  Get the extrema for each array dimension
      Returns:
          {mins: [], maxs: []}
  */
  
  function extrema(arr) {
    var inf = Infinity;
    d = arr[0].length
    n = arr.length,
    mins = [], maxs = [];
    for (var j = d - 1; j >= 0; j--) {
      mins[j] = inf;
      maxs[j] = -inf;
    }
    for (var i = n - 1; i >= 0; i--) {
      for (var j = d - 1; j >= 0; j--) {
        if (arr[i][j] > maxs[j]) {
          maxs[j] = arr[i][j];
        }
        if (arr[i][j] < mins[j]) {
          mins[j] = arr[i][j];
        }
      }
    }
    return {
      mins: mins,
      maxs: maxs
    }
  }
  
  /*  Draw a Canvas sparkline
      parent: id or parent HTML element
      data : [[x,y]]
      w, h : canvas dimensions (px)
  */
  
  function sparkline(parentid, data, baselineVal, w, h) {
    if (data[0].length != 2) {
      console.error('Array must be 2D! (#' + parentid + '), got ' + data[0].length + ')');
      return false;
    }
    if (data.length < 2) {
      console.error('Array must have atlest 2 points! (#' + parentid + ', got ' + data.length + ')');
      return false;
    }
    var canvas,
      ctx,
      parent_elem = document.getElementById(parentid),
      padding = 2, //allow some space for endpoints to show
      lineColor = 'rgba(0,0,0,0.3)',
      baselineColor = 'rgba(5,53,110,0.4)',
      upColor = 'rgba(0,193,0,0.85)',
      downColor = 'rgba(255,50,50,0.85)';
    if (parent_elem.children.length == 0) {
      var canvas = document.createElement('canvas'),
        h = (h ? h : 13),
        w = (w ? w : h * 5);
      canvas.width = w;
      canvas.height = h;
      canvas.id = 'cnv_' + parentid;
      ctx = canvas.getContext('2d');
      ctx.translate(-0.5, -0.5); // Anti aliasing fix.
    } else {
      canvas = parent_elem.children[0];
      ctx = canvas.getContext('2d');
      ctx.clearRect(0, 0, canvas.width, canvas.height); //clear
      ctx.lineWidth = 1; //reset
    }
    var n = data.length,
      extrs = extrema(data),
      rescalex = rescale(extrs.mins[0], extrs.maxs[0], padding, canvas.width - padding),
      rescaley = rescale(extrs.mins[1], extrs.maxs[1], canvas.height - padding, padding),
      x0 = rescalex(data[0][0]),
      y0 = rescaley(data[0][1]),
      twopi = Math.PI * 2;
  
    // Startpoint
    ctx.beginPath();
    ctx.fillStyle = lineColor;
    ctx.arc(x0, y0, 1.25, 0, twopi);
    ctx.fill();
  
    /// Line
    ctx.strokeStyle = lineColor;
    ctx.beginPath();
    ctx.moveTo(x0, y0);
    for (i = 1; i < n; i++) {
      ctx.lineTo(rescalex(data[i][0]), rescaley(data[i][1]));
    }
    ctx.stroke();
  
    // Baseline
    ctx.strokeStyle = baselineColor;
    ctx.lineWidth = 0.5;
    ctx.beginPath();
    var yb = Math.floor(rescaley(baselineVal));
    ctx.moveTo(0, yb);
    ctx.lineTo(canvas.width, yb);
    ctx.stroke();
  
    // Endpoint
    ctx.beginPath();
    ctx.fillStyle = data[n - 1] < data[n - 2] ? downColor : upColor;
    ctx.arc(rescalex(data[n - 1][0]), rescaley(data[n - 1][1]), 1.25, 0, twopi);
    ctx.fill();
  
    if (parent_elem.children.length == 0) {
      parent_elem.appendChild(canvas).id = 'sprkln_' + parentid;
    }
  }
  
  var data_canvas = [
    [1, 3],
    [1.2, 4],
    [3, 1.1],
    [3.01, 12.4],
    [4, 11],
    [4.1, 6.2],
    [5, 3],
    [6, 4],
    [7, 1.1],
    [8, 12.4],
    [9, 11],
    [10.1, 6.2],
    [11, 3],
    [12.2, 4],
    [13, 1.1],
    [14.01, 12.4],
    [15, 11],
    [16.1, 6.2]
  ],
    data_jquery = [3, 4, 1.1, 12.4, 11, 6.2, 3, 4, 1.1, 12.4, 11, 6.2, 3, 4, 1.1, 12.4, 11, 6.2];

};
</script>

Preparation code output

<div class="tckr"><span id="sparkline"></span>12345</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
JQuery
$("#sparkline").sparkline(data_jquery, {
  type: 'line',
  width: '65',
  height: '13',
  lineColor: '#cccccc',
  fillColor: undefined,
  spotColor: '#007f00',
  minSpotColor: undefined,
  maxSpotColor: undefined,
  highlightSpotColor: undefined,
  highlightLineColor: undefined,
  spotRadius: 1.25,
  normalRangeMin: 3,
  normalRangeMax: 3.5
});
pending…
Canvas
sparkline('sparkline', data_canvas, 3);
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

12345