xonecas.io
818
Du

dustymarkdown / files

dustymarkdown / node_modules / dust / benchmark / suites / handlebars_suite.js

2.7 KB
1(function(exports){
2
3var benches = {
4
5  string: {
6    source:  "Hello World!",
7    context: {}
8  },
9
10  replace: {
11    source:  "Hello {{name}}! You have {{count}} new messages.",
12    context: { name: "Mick", count: 30 }
13  },
14
15  array: {
16    source:   "{{#names}}{{name}}{{/names}}",
17    context:  { names: [{name: "Moe"}, {name: "Larry"}, {name: "Curly"}, {name: "Shemp"}] }
18  },
19
20  object: {
21    source:   "{{#person}}{{name}}{{age}}{{/person}}",
22    context:  { person: { name: "Larry", age: 45 } }
23  },
24
25  partial: {
26    source:   "{{#peeps}}{{>replace}}{{/peeps}}",
27    context:  { peeps: [{name: "Moe", count: 15}, {name: "Larry", count: 5}, {name: "Curly", count: 1}] },
28    partials: { replace: "Hello {{name}}! You have {{count}} new messages." }
29  },
30
31  recursion: {
32    source:   "{{name}}{{#kids}}{{>recursion}}{{/kids}}",
33    context:  {
34                name: '1',
35                kids: [
36                  {
37                    name: '1.1',
38                    kids: [
39                      {name: '1.1.1'}
40                    ]
41                  }
42                ]
43              },
44    partials: { recursion: "{{name}}{{#kids}}{{>recursion}}{{/kids}}" }
45  },
46
47  filter: {
48    source:   "{{#filter}}foo {{bar}}{{/filter}}",
49    context:  {
50                filter: function(ctx, fn) {
51                  return fn(ctx).toUpperCase();
52                },
53                bar: "bar"
54              }
55  },
56
57  complex: {
58    source:  "<h1>{{header}}</h1>{{#hasItems}}<ul>{{#items}}{{#current}}" +
59             "<li><strong>{{name}}</strong></li>{{/current}}{{^current}}" +
60             "<li><a href=\"{{url}}\">{{name}}</a></li>{{/current}}"      +
61             "{{/items}}</ul>{{^}}<p>The list is empty.</p>{{/hasItems}}",
62    context: {
63               header: function() {
64                 return "Colors";
65               },
66               items: [
67                 {name: "red", current: true, url: "#Red"},
68                 {name: "green", current: false, url: "#Green"},
69                 {name: "blue", current: false, url: "#Blue"}
70               ],
71               hasItems: function(ctx, fn) {
72                 if (ctx.items.length) {
73                   return fn(ctx);
74                 }
75               }
76             }
77  }
78
79}
80
81exports.handlebarsBench = function(suite, name, id) {
82  var bench = benches[name],
83      fn = Handlebars.compile(bench.source),
84      ctx = bench.context,
85      partials = {};
86
87  if (bench.partials) {
88    for (var key in bench.partials) {
89      partials[key] = Handlebars.compile(bench.partials[key]);
90    }
91  }
92
93  suite.bench(id || name, function(next) {
94    fn(ctx, {partials: partials});
95    next();
96  });
97}
98
99exports.handlebarsBench.benches = benches;
100
101})(typeof exports !== "undefined" ? exports : window);