Permalink
Browse files

Work with callbacks as well as promises or synchronously.

  • Loading branch information...
1 parent a7e4e90 commit 7a76b7ebbac1ada0adf68a700f606a60fee035fe @isaacs committed Jan 22, 2010
Showing with 39 additions and 2 deletions.
  1. +10 −0 README.md
  2. +21 −0 examples/callback-style.js
  3. +8 −2 lib/bench.js
View
10 README.md
@@ -68,6 +68,16 @@ If your test function returns a promise, then the next iteration won't occur unt
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`.
+## Asynchronous Benchmarking without Promises
+
+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.
+
+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.
+
+## Using node-bench programmatically
+
+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.)
+
# <span style="background:red; color:white">WARNING!</span>
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.
View
21 examples/callback-style.js
@@ -0,0 +1,21 @@
+
+// run many times so that we can abstract out the overhead of promise creation.
+var count = 500;
+
+exports.countPerLap = count;
+exports.compare = {
+ nextTick : function (done) {
+ var i = 0;
+ process.nextTick(function () {
+ if (i ++ > count) done();
+ else process.nextTick(arguments.callee);
+ });
+ },
+ setTimeout : function (done) {
+ var i = 0;
+ setTimeout(function () {
+ if (i ++ > count) done();
+ else setTimeout(arguments.callee);
+ });
+ }
+}
View
10 lib/bench.js
@@ -16,7 +16,12 @@ function run (fn, time, countPerLap) {
start = Date.now(),
now = start,
p = new process.Promise(),
- stepsPerLap = 1000;
+ stepsPerLap = 1000,
+ useCallback = (fn.length === 1);
+
+ if (fn.length > 1) throw new Error(
+ "Please read the instructions. Test functions should take 0 or 1 arguments."
+ );
setTimeout(function () { go = false }, time);
function lap () {
@@ -29,7 +34,8 @@ function run (fn, time, countPerLap) {
};
function noop () { runCount ++ };
function runner () {
- var ret = fn();
+ var ret = fn(lap);
+ if (useCallback) return;
if (ret) return ret.addCallback(lap);
// sync case.
for (var i = 0; i < stepsPerLap; i ++) fn();

0 comments on commit 7a76b7e

Please sign in to comment.