summaryrefslogtreecommitdiffstats
path: root/bench/throughput.js
blob: e8ea5d9101db687b5bcc0c8f03af89b03ea264a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var _ = require('underscore'),
    runner = require('./util/template-runner'),

    eco, dust, Handlebars, Mustache;

try {
  dust = require('dustjs-linkedin');
} catch (err) { /* NOP */ }

try {
  Mustache = require('mustache');
} catch (err) { /* NOP */ }

try {
  eco = require('eco');
} catch (err) { /* NOP */ }

function error() {
  throw new Error('EWOT');
}

function makeSuite(bench, name, template, handlebarsOnly) {
  // Create aliases to minimize any impact from having to walk up the closure tree.
  var templateName = name,

      context = template.context,
      partials = template.partials,

      handlebarsOut,
      compatOut,
      dustOut,
      ecoOut,
      mustacheOut;

  var handlebar = Handlebars.compile(template.handlebars, {data: false}),
      compat = Handlebars.compile(template.handlebars, {data: false, compat: true}),
      options = {helpers: template.helpers};
  _.each(template.partials && template.partials.handlebars, function(partial, partialName) {
    Handlebars.registerPartial(partialName, Handlebars.compile(partial, {data: false}));
  });

  handlebarsOut = handlebar(context, options);
  bench('handlebars', function() {
    handlebar(context, options);
  });

  compatOut = compat(context, options);
  bench('compat', function() {
    compat(context, options);
  });

  if (handlebarsOnly) {
    return;
  }

  if (dust) {
    if (template.dust) {
      dustOut = false;
      dust.loadSource(dust.compile(template.dust, templateName));

      dust.render(templateName, context, function(err, out) { dustOut = out; });

      bench('dust', function() {
        dust.render(templateName, context, function() {});
      });
    } else {
      bench('dust', error);
    }
  }

  if (eco) {
    if (template.eco) {
      var ecoTemplate = eco.compile(template.eco);

      ecoOut = ecoTemplate(context);

      bench('eco', function() {
        ecoTemplate(context);
      });
    } else {
      bench('eco', error);
    }
  }

  if (Mustache) {
    var mustacheSource = template.mustache,
        mustachePartials = partials && partials.mustache;

    if (mustacheSource) {
      mustacheOut = Mustache.to_html(mustacheSource, context, mustachePartials);

      bench('mustache', function() {
        Mustache.to_html(mustacheSource, context, mustachePartials);
      });
    } else {
      bench('mustache', error);
    }
  }

  // Hack around whitespace until we have whitespace control
  handlebarsOut = handlebarsOut.replace(/\s/g, '');
  function compare(b, lang) {
    if (b == null) {
      return;
    }

    b = b.replace(/\s/g, '');

    if (handlebarsOut !== b) {
      throw new Error('Template output mismatch: ' + name
            + '\n\nHandlebars: ' + handlebarsOut
            + '\n\n' + lang + ': ' + b);
    }
  }

  compare(compatOut, 'compat');
  compare(dustOut, 'dust');
  compare(ecoOut, 'eco');
  compare(mustacheOut, 'mustache');
}

module.exports = function(grunt, callback) {
  // Deferring load incase we are being run inline with the grunt build
  Handlebars = require('../lib');

  console.log('Execution Throughput');
  runner(grunt, makeSuite, function(times, scaled) {
    callback(scaled);
  });
};