Skip to content

Commit

Permalink
Optimize the compiler loop
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Dec 14, 2015
1 parent 26d3d78 commit eeca363
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/js/utils/compiler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { forEach } from './array-utils';

export function visit(visitor, node, opcodes) {
const method = node.type;
if (!visitor[method]) {
Expand All @@ -9,10 +11,15 @@ export function visit(visitor, node, opcodes) {
export function compile(compiler, opcodes) {
for (var i=0, l=opcodes.length; i<l; i++) {
let [method, ...params] = opcodes[i];
if (params.length) {
compiler[method].apply(compiler, params);
} else {
let length = params.length;
if (length === 0) {
compiler[method].call(compiler);
} else if (length === 1) {
compiler[method].call(compiler, params[0]);
} else if (length === 2) {
compiler[method].call(compiler, params[0], params[1]);
} else {
compiler[method].apply(compiler, params);
}
}
}
Expand All @@ -21,7 +28,7 @@ export function visitArray(visitor, nodes, opcodes) {
if (!nodes || nodes.length === 0) {
return;
}
nodes.forEach(node => {
forEach(nodes, node => {
visit(visitor, node, opcodes);
});
}

0 comments on commit eeca363

Please sign in to comment.