Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 433e06c06623d6d7b6af3482692a9167db6a4292 1 parent b695e9e
Elijah Insua authored November 21, 2011
36  lib/conductor.js
@@ -66,7 +66,7 @@ OTHER DEALINGS IN THE SOFTWARE.
66 66
       this.emit = false;
67 67
     } else if (parent && parent.emit) {
68 68
       this.emit = parent.emit;
69  
-    } else {
  69
+    } else if (conductor) {
70 70
       this.emit = function(name, data) {
71 71
         if (conductor.listeners[name]) {
72 72
           var i, listener;
@@ -178,8 +178,10 @@ OTHER DEALINGS IN THE SOFTWARE.
178 178
   };
179 179
 
180 180
   Edge.prototype = {
  181
+    length : 0,
  182
+
181 183
     // Getter for the current number of ports in this edge
182  
-    length : function () {
  184
+    /*length : function () {
183 185
       var l = 0, p;
184 186
       for (p in this.ports) {
185 187
         if (this.ports.hasOwnProperty(p)) {
@@ -187,7 +189,7 @@ OTHER DEALINGS IN THE SOFTWARE.
187 189
         }
188 190
       }
189 191
       return l;
190  
-    },
  192
+    },*/
191 193
 
192 194
     // Mark satisfaction in the current Context, and attempt to trigger this edge
193 195
     satisfy : function (context, port, value) {
@@ -314,7 +316,7 @@ OTHER DEALINGS IN THE SOFTWARE.
314 316
       context.prepare(this.node);
315 317
 
316 318
       // Don't bother with the processing if there are no ports to satisfy.
317  
-      if (this.length() === 0) {
  319
+      if (this.length === 0) {
318 320
         if (context.emit) { context.emit("edge.satisfied.end", { edge: this, context: context, result: true }); }
319 321
         return true;
320 322
       }
@@ -359,17 +361,21 @@ OTHER DEALINGS IN THE SOFTWARE.
359 361
       return true;
360 362
     },
361 363
 
  364
+    addPort : function(index) {
  365
+      if (!this.ports[index]) {
  366
+        this.ports[index] = new Port(this, index);
  367
+        this.length += 1;
  368
+      }
  369
+      return this.ports[index];
  370
+    },
  371
+
362 372
     // This is an internal method to setup a route from a port on the current Edge to another port. This
363 373
     // connection is described as a "pipe" internally.
364 374
     route : function (index, port) {
365 375
       // When no port is passed, this method acts as a getter
366 376
       if (!port) {
367 377
         // If the port thats being queried does not exist, create it
368  
-        if (!this.ports[index]) {
369  
-          this.ports[index] = new Port(this, index);
370  
-        }
371  
-
372  
-        return this.ports[index] || null;
  378
+        return this.addPort(index);
373 379
       }
374 380
 
375 381
       // Passing an input into an input means "callback"
@@ -378,9 +384,7 @@ OTHER DEALINGS IN THE SOFTWARE.
378 384
       }
379 385
 
380 386
       // If the port in question does not exist, create it
381  
-      if (!this.ports[index]) {
382  
-        this.ports[index] = new Port(this, index);
383  
-      }
  387
+      this.addPort(index)
384 388
 
385 389
       // Wrap callbacks with a new node and attach to the appropriate port on this Edge
386 390
       if (this.type === "output" && typeof port === 'function') {
@@ -518,14 +522,14 @@ OTHER DEALINGS IN THE SOFTWARE.
518 522
     // This method returns true when the number of inputs is greater than one which means
519 523
     // there are multiple sources "joining" at this node.
520 524
     this.isJoin = function () {
521  
-      return (this.edges.input.length() > 1);
  525
+      return (this.edges.input.length > 1);
522 526
     };
523 527
 
524 528
     // This method returns true if the number of outputs + callbacks is greater than 1.  The reason for
525 529
     // this is: it is impossible to have multiple outputs if not by callbacks (or events)
526 530
     this.isFork = function () {
527  
-      return (this.edges.callback.length() > 1 ||
528  
-              this.edges.callback.length() + this.edges.output.length() > 1);
  531
+      return (this.edges.callback.length > 1 ||
  532
+              this.edges.callback.length + this.edges.output.length > 1);
529 533
     };
530 534
 
531 535
     // Execute the function that this node wraps.
@@ -544,7 +548,7 @@ OTHER DEALINGS IN THE SOFTWARE.
544 548
           // The only output you can actually have is a return.. everything else
545 549
           // is callback based.  The only reason we'd need to satisfy it is if it's
546 550
           // required in the current flow.
547  
-          if (self.edges.output.length() > 0) {
  551
+          if (self.edges.output.length > 0) {
548 552
             self.edges.output.satisfy(context, 0, result);
549 553
           }
550 554
           if (context.emit) { context.emit("node.execute.perform.end", { node: this, context: context }); }
14  test/test.js
@@ -67,9 +67,9 @@ ok("Reparented nodes should still be leafs",
67 67
    reparented.parent.forkNode === true);
68 68
 
69 69
 ok("a node's edges are empty by default",
70  
-   sanityNode.edges.input.length() === 0 &&
71  
-   sanityNode.edges.output.length() === 0 &&
72  
-   sanityNode.edges.callback.length() === 0);
  70
+   sanityNode.edges.input.length === 0 &&
  71
+   sanityNode.edges.output.length === 0 &&
  72
+   sanityNode.edges.callback.length === 0);
73 73
 
74 74
 ok("empty edges should be satisfied by default",
75 75
    sanityNode.edges.input.satisfied(sanityContext) &&
@@ -408,21 +408,21 @@ uf.execute();
408 408
 
409 409
 // Function argument parsing tests
410 410
 var fapt        = conductor(),
411  
-    newline     = fapt.node(function(a, b,   
  411
+    newline     = fapt.node(function(a, b,
412 412
                   c) {}),
413 413
     lineComment = fapt.node(function(a // this is a comment
414 414
                             ,b){}),
415 415
     multiComment = fapt.node(function(a /*
416 416
       this is a multiline comment
417 417
     */, b, /* single line comment */c) {});
418  
-    
  418
+
419 419
 ok("parsing new lines should not be a problem",
420 420
     newline.args.a && newline.args.b && newline.args.c);
421 421
 
422  
-ok("parsing line comments should not be a problem", 
  422
+ok("parsing line comments should not be a problem",
423 423
   lineComment.args.a && lineComment.args.b);
424 424
 
425  
-ok("parsing multi-line comments should not be a problem", 
  425
+ok("parsing multi-line comments should not be a problem",
426 426
   multiComment.args.a && multiComment.args.b && multiComment.args.c);
427 427
 
428 428
 //

0 notes on commit 433e06c

Please sign in to comment.
Something went wrong with that request. Please try again.