Skip to content

Instantly share code, notes, and snippets.

@bjouhier
Created April 11, 2012 20:01

Revisions

  1. bjouhier revised this gist Apr 11, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion benchCallbacks.js
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ function bench(cb) {
    cb(null, total);
    } else {
    load(__dirname + '/benchCallbacks.js', function(err, data) {
    if (err) cb(err);
    if (err) return cb(err);
    total += data.length;
    loop(i + 1);
    })
  2. bjouhier revised this gist Apr 11, 2012. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion benchCallbacks.js
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ function bench(cb) {
    if (i === count) {
    cb(null, total);
    } else {
    load(__dirname + '/benchCb.js', function(err, data) {
    load(__dirname + '/benchCallbacks.js', function(err, data) {
    if (err) cb(err);
    total += data.length;
    loop(i + 1);
    2 changes: 1 addition & 1 deletion benchStreamline._js
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ var count = 1000000;
    function bench(_) {
    var total = 0;
    for (var i = 0; i < count; i++) {
    var res = load(__dirname + '/benchCb.js', _);
    var res = load(__dirname + '/benchCallbacks.js', _);
    total += res.length;
    }
    return total;
  3. bjouhier created this gist Apr 11, 2012.
    44 changes: 44 additions & 0 deletions benchCallbacks.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    "use strict";
    var fs = require('fs');
    var cache = {}, hit = 0, missed = 0;

    function load(name, cb) {
    var res = cache[name];
    if (res) {
    process.nextTick(function() {
    hit++;
    cb(null, res);
    });
    } else {
    fs.readFile(name, function(err, data) {
    missed++;
    cb(null, cache[name] = data);
    });
    }
    }

    var count = 1000000;

    function bench(cb) {
    var total = 0;
    function loop(i) {
    if (i === count) {
    cb(null, total);
    } else {
    load(__dirname + '/benchCb.js', function(err, data) {
    if (err) cb(err);
    total += data.length;
    loop(i + 1);
    })
    }
    }
    loop(0);
    }


    var t0 = Date.now();
    bench(function(err, result) {
    if (err) throw err;
    console.log('hit=' + hit + ', missed=' + missed + ', result=' + result);
    console.log('elapsed: ' + (Date.now() - t0));
    });
    30 changes: 30 additions & 0 deletions benchStreamline._js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    "use strict";
    var fs = require('fs');
    var cache = {}, hit = 0, missed = 0;

    function load(name, _) {
    var res = cache[name];
    if (res) {
    hit++;
    return res;
    } else {
    missed++;
    return cache[name] = fs.readFile(name, _);
    }
    }

    var count = 1000000;

    function bench(_) {
    var total = 0;
    for (var i = 0; i < count; i++) {
    var res = load(__dirname + '/benchCb.js', _);
    total += res.length;
    }
    return total;
    }

    var t0 = Date.now();
    var result = bench(_);
    console.log('hit=' + hit + ', missed=' + missed + ', result=' + result);
    console.log('elapsed: ' + (Date.now() - t0));