ybogdanov/node-sync

performance

dvv opened this issue · 1 comments

dvv commented

Hi! The following:

    var Sync = require('sync');

    function worker(a, b, callback) {
      process.nextTick(function() {
        callback(null, a + b);
      });
    }

    var t0 = new Date;

    var n = 100000;
    function run(err, result) {
      if (!n) {
        console.log('ASYNC', result, new Date - t0);
        t0 = new Date;
        Sync(function() {
          var result = 0;
          for (var n = 100000; --n >= 0; ) {
            result = worker.sync(null, 0, n + result);
          }
          console.log('SYNC', result, new Date - t0);
          process.exit(0);
        });
      }
      --n;
      worker(0, result + n, run);
    }
    run(null, 0);

shows sync is slower circa 5-6 times than async. Please, validate/void this example and results.

node 0.6.3 under Linux dvv 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:50:42 UTC 2011 i686 i686 i386 GNU/Linux

TIA,
--Vladimir

Hi Vladimir,

Yes, the benchmark script looks correct. But you should compare performance against plain node-fibers implementation, because node-sync is just wrapper which adds some sugar.

var Sync = require('..');

function worker(a, b, callback) {
  process.nextTick(function() {
    callback(null, a + b);
  });
}

var t0 = new Date;

var n = 100000;
function run(err, result) {
  if (!n) {
    console.log('ASYNC', result, new Date - t0);
    // test sync
    t0 = new Date;
    Sync(function() {
      var result = 0;
      for (var n = 100000; --n >= 0; ) {
        result = worker.sync(null, 0, n + result);
      }
      console.log('SYNC', result, new Date - t0);

      // test plain fibers
      t0 = new Date;
      Fiber(function() {
        var fiber = Fiber.current;
        var result = 0;
        for (var n = 100000; --n >= 0; ) {
          worker(0, n + result, function(err, result){
              fiber.run(result);
          });
          result = Fiber.yield();
        }
        console.log('FIBERS', result, new Date - t0);
        process.exit(0);
      }).run();
    });
  }
  --n;
  worker(0, result + n, run);
}
run(null, 0);

My result:

ASYNC 4999950000 219
SYNC 4999950000 774
FIBERS 4999950000 615