Permalink
Browse files

Fix: don't recompute length every time (5200 req/s)

  • Loading branch information...
1 parent b695e9e commit 433e06c06623d6d7b6af3482692a9167db6a4292 @tmpvar committed Nov 22, 2011
Showing with 27 additions and 23 deletions.
  1. +20 −16 lib/conductor.js
  2. +7 −7 test/test.js
View
@@ -66,7 +66,7 @@ OTHER DEALINGS IN THE SOFTWARE.
this.emit = false;
} else if (parent && parent.emit) {
this.emit = parent.emit;
- } else {
+ } else if (conductor) {
this.emit = function(name, data) {
if (conductor.listeners[name]) {
var i, listener;
@@ -178,16 +178,18 @@ OTHER DEALINGS IN THE SOFTWARE.
};
Edge.prototype = {
+ length : 0,
+
// Getter for the current number of ports in this edge
- length : function () {
+ /*length : function () {
var l = 0, p;
for (p in this.ports) {
if (this.ports.hasOwnProperty(p)) {
l++;
}
}
return l;
- },
+ },*/
// Mark satisfaction in the current Context, and attempt to trigger this edge
satisfy : function (context, port, value) {
@@ -314,7 +316,7 @@ OTHER DEALINGS IN THE SOFTWARE.
context.prepare(this.node);
// Don't bother with the processing if there are no ports to satisfy.
- if (this.length() === 0) {
+ if (this.length === 0) {
if (context.emit) { context.emit("edge.satisfied.end", { edge: this, context: context, result: true }); }
return true;
}
@@ -359,17 +361,21 @@ OTHER DEALINGS IN THE SOFTWARE.
return true;
},
+ addPort : function(index) {
+ if (!this.ports[index]) {
+ this.ports[index] = new Port(this, index);
+ this.length += 1;
+ }
+ return this.ports[index];
+ },
+
// This is an internal method to setup a route from a port on the current Edge to another port. This
// connection is described as a "pipe" internally.
route : function (index, port) {
// When no port is passed, this method acts as a getter
if (!port) {
// If the port thats being queried does not exist, create it
- if (!this.ports[index]) {
- this.ports[index] = new Port(this, index);
- }
-
- return this.ports[index] || null;
+ return this.addPort(index);
}
// Passing an input into an input means "callback"
@@ -378,9 +384,7 @@ OTHER DEALINGS IN THE SOFTWARE.
}
// If the port in question does not exist, create it
- if (!this.ports[index]) {
- this.ports[index] = new Port(this, index);
- }
+ this.addPort(index)
// Wrap callbacks with a new node and attach to the appropriate port on this Edge
if (this.type === "output" && typeof port === 'function') {
@@ -518,14 +522,14 @@ OTHER DEALINGS IN THE SOFTWARE.
// This method returns true when the number of inputs is greater than one which means
// there are multiple sources "joining" at this node.
this.isJoin = function () {
- return (this.edges.input.length() > 1);
+ return (this.edges.input.length > 1);
};
// This method returns true if the number of outputs + callbacks is greater than 1. The reason for
// this is: it is impossible to have multiple outputs if not by callbacks (or events)
this.isFork = function () {
- return (this.edges.callback.length() > 1 ||
- this.edges.callback.length() + this.edges.output.length() > 1);
+ return (this.edges.callback.length > 1 ||
+ this.edges.callback.length + this.edges.output.length > 1);
};
// Execute the function that this node wraps.
@@ -544,7 +548,7 @@ OTHER DEALINGS IN THE SOFTWARE.
// The only output you can actually have is a return.. everything else
// is callback based. The only reason we'd need to satisfy it is if it's
// required in the current flow.
- if (self.edges.output.length() > 0) {
+ if (self.edges.output.length > 0) {
self.edges.output.satisfy(context, 0, result);
}
if (context.emit) { context.emit("node.execute.perform.end", { node: this, context: context }); }
View
@@ -67,9 +67,9 @@ ok("Reparented nodes should still be leafs",
reparented.parent.forkNode === true);
ok("a node's edges are empty by default",
- sanityNode.edges.input.length() === 0 &&
- sanityNode.edges.output.length() === 0 &&
- sanityNode.edges.callback.length() === 0);
+ sanityNode.edges.input.length === 0 &&
+ sanityNode.edges.output.length === 0 &&
+ sanityNode.edges.callback.length === 0);
ok("empty edges should be satisfied by default",
sanityNode.edges.input.satisfied(sanityContext) &&
@@ -408,21 +408,21 @@ uf.execute();
// Function argument parsing tests
var fapt = conductor(),
- newline = fapt.node(function(a, b,
+ newline = fapt.node(function(a, b,
c) {}),
lineComment = fapt.node(function(a // this is a comment
,b){}),
multiComment = fapt.node(function(a /*
this is a multiline comment
*/, b, /* single line comment */c) {});
-
+
ok("parsing new lines should not be a problem",
newline.args.a && newline.args.b && newline.args.c);
-ok("parsing line comments should not be a problem",
+ok("parsing line comments should not be a problem",
lineComment.args.a && lineComment.args.b);
-ok("parsing multi-line comments should not be a problem",
+ok("parsing multi-line comments should not be a problem",
multiComment.args.a && multiComment.args.b && multiComment.args.c);
//

0 comments on commit 433e06c

Please sign in to comment.