Skip to content

Commit

Permalink
Filter out loop statements in operator overloading code, to optimize …
Browse files Browse the repository at this point in the history
…loop execution speed.
  • Loading branch information
lehni committed Jan 5, 2014
1 parent 6ad2105 commit cd76049
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/core/PaperScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,17 @@ var PaperScript = Base.exports.PaperScript = (function() {
}
}
switch (node && node.type) {
case 'UnaryExpression':
// -a
if (node.operator in unaryOperators
&& node.argument.type !== 'Literal') {
var arg = getCode(node.argument);
replaceCode(node, '$_("' + node.operator + '", '
+ arg + ')');
}
break;
case 'BinaryExpression':
// a + b, a - b, a / b, a * b, a == b, a % b, ...
if (node.operator in binaryOperators
&& node.left.type !== 'Literal') {
var left = getCode(node.left),
Expand All @@ -177,22 +187,17 @@ var PaperScript = Base.exports.PaperScript = (function() {
+ '", ' + right + ')');
}
break;
case 'AssignmentExpression':
if (/^.=$/.test(node.operator)
&& node.left.type !== 'Literal') {
var left = getCode(node.left),
right = getCode(node.right);
replaceCode(node, left + ' = _$_(' + left + ', "'
+ node.operator[0] + '", ' + right + ')');
}
break;
case 'UpdateExpression':
// a++, a--
if (!node.prefix && !(parent && (
// Filter out for statements to allow loop increments
// to perform well
parent.type === 'ForStatement'
// We need to filter out parents that are comparison
// operators, e.g. for situations like if (++i < 1),
// as we can't replace that with if (_$_(i, "+", 1) < 1)
// Match any operator beginning with =, !, < and >.
parent.type === 'BinaryExpression'
|| parent.type === 'BinaryExpression'
&& /^[=!<>]/.test(parent.operator)
// array[i++] is a MemberExpression with computed = true
// We can't replace that with array[_$_(i, "+", 1)].
Expand All @@ -203,12 +208,15 @@ var PaperScript = Base.exports.PaperScript = (function() {
+ node.operator[0] + '", 1)');
}
break;
case 'UnaryExpression':
if (node.operator in unaryOperators
&& node.argument.type !== 'Literal') {
var arg = getCode(node.argument);
replaceCode(node, '$_("' + node.operator + '", '
+ arg + ')');
case 'AssignmentExpression':
/// a += b, a -= b
if (/^.=$/.test(node.operator)
&& node.left.type !== 'Literal'
&& !(parent && parent.type === 'ForStatement')) {
var left = getCode(node.left),
right = getCode(node.right);
replaceCode(node, left + ' = _$_(' + left + ', "'
+ node.operator[0] + '", ' + right + ')');
}
break;
}
Expand Down

0 comments on commit cd76049

Please sign in to comment.