Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Work with callbacks as well as promises or synchronously.

  • Loading branch information...
commit 7a76b7ebbac1ada0adf68a700f606a60fee035fe 1 parent a7e4e90
Isaac Z. Schlueter authored January 21, 2010
10  README.md
Source Rendered
@@ -68,6 +68,16 @@ If your test function returns a promise, then the next iteration won't occur unt
68 68
 
69 69
 Note that creating and returning a promise involves extra overhead.  So, if you're trying to compare something where the overall time per iteration is around the same as the time to create a promise, then this will lead to useless or misleading results.  To get around this problem, run your function many times before emitting success on the promise.  As an example, check out `examples/nexttick-vs-settimeout.js`.
70 70
 
  71
+## Asynchronous Benchmarking without Promises
  72
+
  73
+If you prefer callbacks to promises, that's fine.  Just write your functions so that they take a single argument.  That argument is your callback.  Have fun with it.
  74
+
  75
+Your callback will be fired using `process.nextTick`.  This has a wee bit of overhead, so if you're testing something really fast, you should probably construct it to run many times before calling the callback.  Check the `examples/callback-style.js` test for an example.
  76
+
  77
+## Using node-bench programmatically
  78
+
  79
+Put the `lib/bench.js` file in your `$NODE_PATH` somewhere, and then do `require("bench")` to access its goodies.  (Or put it somewhere else, and require it from there.)
  80
+
71 81
 # <span style="background:red; color:white">WARNING!</span>
72 82
 
73 83
 Statistics are powerful tools, and in the wrong hands, can lead to a lot of mayhem.  Please use this tool for good, and not evil.
21  examples/callback-style.js
... ...
@@ -0,0 +1,21 @@
  1
+
  2
+// run many times so that we can abstract out the overhead of promise creation.
  3
+var count = 500;
  4
+
  5
+exports.countPerLap = count;
  6
+exports.compare = {
  7
+  nextTick : function (done) {
  8
+    var i = 0;
  9
+    process.nextTick(function () {
  10
+      if (i ++ > count) done();
  11
+      else process.nextTick(arguments.callee);
  12
+    });
  13
+  },
  14
+  setTimeout : function (done) {
  15
+    var i = 0;
  16
+    setTimeout(function () {
  17
+      if (i ++ > count) done();
  18
+      else setTimeout(arguments.callee);
  19
+    });
  20
+  }
  21
+}
10  lib/bench.js
@@ -16,7 +16,12 @@ function run (fn, time, countPerLap) {
16 16
     start = Date.now(),
17 17
     now = start,
18 18
     p = new process.Promise(),
19  
-    stepsPerLap = 1000;
  19
+    stepsPerLap = 1000,
  20
+    useCallback = (fn.length === 1);
  21
+  
  22
+  if (fn.length > 1) throw new Error(
  23
+    "Please read the instructions.  Test functions should take 0 or 1 arguments."
  24
+  );
20 25
   
21 26
   setTimeout(function () { go = false }, time);
22 27
   function lap () {
@@ -29,7 +34,8 @@ function run (fn, time, countPerLap) {
29 34
   };
30 35
   function noop () { runCount ++ };
31 36
   function runner () {
32  
-    var ret = fn();
  37
+    var ret = fn(lap);
  38
+    if (useCallback) return;
33 39
     if (ret) return ret.addCallback(lap);
34 40
     // sync case.
35 41
     for (var i = 0; i < stepsPerLap; i ++) fn();

0 notes on commit 7a76b7e

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