Permalink
Browse files

Fix for speed test decrease caused by loop.cycle

Noticed that the change in 2319405 that added the `loop.cycle` option dramatically reduced render speed. For some reason, defining a function on the `_context` reduces the speed. I'm not sure why--would be good to also investigate why that happens, as this is more or less doing the same thing, just adding the function to a different object.

Conflicts:

	lib/tags.js
  • Loading branch information...
1 parent 0ea655d commit c553233558cf35fe283a97a432ea1206b734bfdd @paularmstrong paularmstrong committed Feb 17, 2012
Showing with 17 additions and 11 deletions.
  1. +7 −5 lib/helpers.js
  2. +4 −0 lib/parser.js
  3. +6 −6 lib/tags/for.js
View
@@ -229,11 +229,13 @@ exports.wrapMethod = function (variable, filter, context) {
});
args = (args && args.length) ? args.join(',') : '""';
- if (args.length) {
- output += 'return ' + context + '["' + filter.name + '"].call(this, ' + args + ');\n';
- } else {
- output += 'return ' + context + '["' + filter.name + '"].call(this);\n';
- }
+ output += 'return ';
+ output += (context) ? context + '["' : '';
+ output += filter.name;
+ output += (context) ? '"]' : '';
+ output += '.call(this';
+ output += (args.length) ? ', ' + args : '';
+ output += ');\n';
return output + '})()';
};
View
@@ -378,6 +378,10 @@ exports.compile = function compile(indent, parentBlock) {
code += 'if (typeof _context !== "undefined" && typeof _context' + key + ' === "function") {\n';
code += ' _output += ' + helpers.wrapMethod('', { name: name, args: args }, '_context') + ';\n';
+ if (helpers.isValidName(name)) {
+ code += '} else if (typeof ' + name + ' === "function") {\n';
+ code += ' _output += ' + helpers.wrapMethod('', { name: name, args: args }) + ';\n';
+ }
code += '} else {\n';
code += helpers.setVar('__' + name, token);
code += ' _output += __' + name + ';\n';
View
@@ -40,11 +40,11 @@ module.exports = function (indent, parentBlock, parser) {
out = '(function () {\n' +
' var loop = {}, __loopKey, __loopIndex = 0, __loopLength = 0,' +
' __ctx_operand = _context["' + operand1 + '"],\n' +
- ' __ctx_cycle = _context.loop_cycle;\n' +
- '_context.loop_cycle = function() {\n' +
- ' var args = _.toArray(arguments), i = loop.index0 % args.length;\n' +
- ' return args[i];\n' +
- '};\n' +
+ ' __ctx_cycle = (typeof loop_cycle !== "undefined") ? loop_cycle : null,\n' +
+ ' loop_cycle = function() {\n' +
+ ' var args = _.toArray(arguments), i = loop.index0 % args.length;\n' +
+ ' return args[i];\n' +
+ ' };\n' +
helpers.setVar('__loopIter', operand2) +
' else {\n' +
' return;\n' +
@@ -65,7 +65,7 @@ module.exports = function (indent, parentBlock, parser) {
loopShared +
' }\n' +
' }\n' +
- ' _context.loop_cycle = __ctx_cycle;\n' +
+ ' loop_cycle = __ctx_cycle;\n' +
' _context["' + operand1 + '"] = __ctx_operand;\n' +
'})();\n';

0 comments on commit c553233

Please sign in to comment.