Skip to content

Instantly share code, notes, and snippets.

@zenlor
Created December 26, 2011 15:35
Show Gist options
  • Select an option

  • Save zenlor/1521429 to your computer and use it in GitHub Desktop.

Select an option

Save zenlor/1521429 to your computer and use it in GitHub Desktop.

Revisions

  1. zenlor revised this gist Jul 3, 2012. 5 changed files with 35 additions and 28 deletions.
    14 changes: 1 addition & 13 deletions Benchmark-ish.js
    Original file line number Diff line number Diff line change
    @@ -81,17 +81,5 @@ Step(
    });
    }
    , function () {
    console.log('\n\nJS::::'.magenta)
    console.log(js)
    console.log('JS::::'.green)

    console.log('\n\nEX::::'.magenta)
    console.log(ex)
    console.log('EX::::'.green)

    console.log('\n\nSP::::'.magenta)
    console.log(sp)
    console.log('SP::::'.green)

    process.exit(); }
    );
    );
    5 changes: 0 additions & 5 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    in my node_modules folder ...

    walk() Finished in: 30 ms
    exec('find ...') Finished in: 14 ms
    spawn('find ...') Finished in: 6 ms
    15 changes: 15 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    in my node_modules folder ...

    walk() Finished in: 30 ms
    exec('find ...') Finished in: 14 ms
    spawn('find ...') Finished in: 6 ms


    using node 0.8.1

    walk() Finished in: 17 ms
    exec('find ...') Finished in: 8 ms
    spawn('find ...') Finished in: 5 ms


    results may vary
    19 changes: 19 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    {
    "name": "gist-1521429",
    "version": "0.0.0",
    "main": "Benchmark-ish.js",
    "dependencies": {},
    "devDependencies": {
    "colors": "~0.6.0-1",
    "express": "~2.5.11",
    "step": "~0.0.5"
    },
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
    },
    "repository": {
    "type": "git",
    "url": "git://gist.github.com/1521429.git"
    },
    "license": "BSD"
    }
    Original file line number Diff line number Diff line change
    @@ -1,10 +0,0 @@
    require("./walk").walk(
    "/path/to/a/directory",
    function(file) {
    console.log(file);
    },
    function(err) {
    if(err) throw err;
    console.log("Finished.");
    }
    );
  2. zenlor revised this gist Dec 26, 2011. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions Benchmark-ish.js
    Original file line number Diff line number Diff line change
    @@ -60,7 +60,7 @@ Step(
    var self = this;
    start = Date.now();

    cp.exec('find ./node_modules', function(error, stdout, stderr) {
    cp.exec('find ./node_modules -type f', function(error, stdout, stderr) {
    if (error) throw error;
    console.log("exec('find ...') Finished in: %d ms".red, (Date.now()) - start);
    ex = stdout;
    @@ -71,7 +71,7 @@ Step(
    var self = this;
    start = Date.now();

    var cmd = cp.spawn('find', ['./node_modules']);
    var cmd = cp.spawn('find', ['./node_modules', '-type', 'f']);
    cmd.stdout.on('data', function (data) {
    sp += data;
    });
    @@ -90,8 +90,8 @@ Step(
    console.log('EX::::'.green)

    console.log('\n\nSP::::'.magenta)
    console.log(js)
    console.log(sp)
    console.log('SP::::'.green)

    process.exit(); }
    );
    );
  3. zenlor revised this gist Dec 26, 2011. No changes.
  4. zenlor revised this gist Dec 26, 2011. 3 changed files with 112 additions and 0 deletions.
    97 changes: 97 additions & 0 deletions Benchmark-ish.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    var fs = require('fs')
    , path = require("path")
    , cp = require('child_process')
    , Step = require('step')
    , colors = require('colors')
    , start = Date.now();

    walk = (function() {
    var counter = 0;
    var walk = function(dirname, callback, finished) {
    counter += 1;
    fs.readdir(dirname, function(err, relnames) {
    if(err) {
    finished(err);
    return;
    }
    relnames.forEach(function(relname, index, relnames) {
    var name = path.join(dirname, relname);
    counter += 1;
    fs.lstat(name, function(err, stat) {
    if(err) {
    finished(err);
    return;
    }
    if(stat.isDirectory()) {
    walk(name, callback, finished);
    } else {
    callback(name);
    }
    counter -= 1;
    if(index === relnames.length - 1) counter -= 1;
    if(counter === 0) {
    finished(null);
    }
    });
    });
    });
    };
    return walk;
    })();

    var js = ex = sp = '';

    Step(
    function () {
    var self = this;
    walk(
    "./node_modules"
    , function(data) {
    js += (data + '\n');
    }
    , function(err) {
    if(err) throw err;
    console.log("walk() Finished in: %d ms".red, (Date.now()) - start);
    return self();
    }
    )
    }
    , function () {
    var self = this;
    start = Date.now();

    cp.exec('find ./node_modules', function(error, stdout, stderr) {
    if (error) throw error;
    console.log("exec('find ...') Finished in: %d ms".red, (Date.now()) - start);
    ex = stdout;
    self();
    });
    }
    , function () {
    var self = this;
    start = Date.now();

    var cmd = cp.spawn('find', ['./node_modules']);
    cmd.stdout.on('data', function (data) {
    sp += data;
    });
    cmd.on('exit', function () {
    console.log("spawn('find ...') Finished in: %d ms".red, (Date.now()) - start);
    self();
    });
    }
    , function () {
    console.log('\n\nJS::::'.magenta)
    console.log(js)
    console.log('JS::::'.green)

    console.log('\n\nEX::::'.magenta)
    console.log(ex)
    console.log('EX::::'.green)

    console.log('\n\nSP::::'.magenta)
    console.log(js)
    console.log('SP::::'.green)

    process.exit(); }
    );
    5 changes: 5 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    in my node_modules folder ...

    walk() Finished in: 30 ms
    exec('find ...') Finished in: 14 ms
    spawn('find ...') Finished in: 6 ms
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    require("./walk").walk(
    "/path/to/a/directory",
    function(file) {
    console.log(file);
    },
    function(err) {
    if(err) throw err;
    console.log("Finished.");
    }
    );
  5. stygstra revised this gist Aug 9, 2010. 3 changed files with 46 additions and 44 deletions.
    44 changes: 0 additions & 44 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,44 +0,0 @@
    var http = require('http'),
    sys = require('sys'),
    path = require('path'),
    posix = require('posix'),
    events = require('events');

    var listdir = function (pathname) {
    var p = new events.Promise();
    var ls = process.createChildProcess("ls", [pathname]);
    ls.addListener("output", function (data) {
    if (data) {
    var files = [];
    data.split('\n').forEach(function(f) {if (f){files.push(f)}});
    p.emitSuccess(files)
    }
    });
    return p;
    }

    var walk = function (pathname, callback) {
    var counter=1;
    var promise = new process.Promise();
    go(pathname);
    return promise;
    function go(pathname){
    listdir(pathname).addCallback(function(files) {
    counter--;
    files.forEach(function(f) {
    var abspath = path.join(pathname, f);
    posix.stat(abspath).addCallback(function(stat) {
    if (stat.isDirectory()) {
    counter++;
    go(abspath);
    } else {
    callback(abspath);
    }
    });
    });
    if(!counter) promise.emitSuccess();
    });
    }
    }

    walk('/home/node', function() {});
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    require("./walk").walk(
    "/path/to/a/directory",
    function(file) {
    console.log(file);
    },
    function(err) {
    if(err) throw err;
    console.log("Finished.");
    }
    );
    36 changes: 36 additions & 0 deletions walk.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    var fs = require("fs");
    var path = require("path");

    exports.walk = (function() {
    var counter = 0;
    var walk = function(dirname, callback, finished) {
    counter += 1;
    fs.readdir(dirname, function(err, relnames) {
    if(err) {
    finished(err);
    return;
    }
    relnames.forEach(function(relname, index, relnames) {
    var name = path.join(dirname, relname);
    counter += 1;
    fs.stat(name, function(err, stat) {
    if(err) {
    finished(err);
    return;
    }
    if(stat.isDirectory()) {
    exports.walk(name, callback, finished);
    } else {
    callback(name);
    }
    counter -= 1;
    if(index === relnames.length - 1) counter -= 1;
    if(counter === 0) {
    finished(null);
    }
    });
    });
    });
    };
    return walk;
    })();
  6. @inimino inimino revised this gist Feb 3, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ var listdir = function (pathname) {
    var walk = function (pathname, callback) {
    var counter=1;
    var promise = new process.Promise();
    go();
    go(pathname);
    return promise;
    function go(pathname){
    listdir(pathname).addCallback(function(files) {
    @@ -30,7 +30,7 @@ var walk = function (pathname, callback) {
    posix.stat(abspath).addCallback(function(stat) {
    if (stat.isDirectory()) {
    counter++;
    walk(abspath, callback);
    go(abspath);
    } else {
    callback(abspath);
    }
  7. @inimino inimino revised this gist Feb 3, 2010. 1 changed file with 20 additions and 21 deletions.
    41 changes: 20 additions & 21 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -17,29 +17,28 @@ var listdir = function (pathname) {
    return p;
    }

    var counter = 0;
    var walk = function (pathname, callback) {
    counter++;
    listdir(pathname).addCallback(function(files) {
    var finished = true;
    files.forEach(function(f) {
    var abspath = path.join(pathname, f);
    posix.stat(abspath).addCallback(function(stat) {
    if (stat.isDirectory()) {
    finished = false;
    walk(abspath, callback);
    } else {
    callback(abspath);
    }
    var counter=1;
    var promise = new process.Promise();
    go();
    return promise;
    function go(pathname){
    listdir(pathname).addCallback(function(files) {
    counter--;
    files.forEach(function(f) {
    var abspath = path.join(pathname, f);
    posix.stat(abspath).addCallback(function(stat) {
    if (stat.isDirectory()) {
    counter++;
    walk(abspath, callback);
    } else {
    callback(abspath);
    }
    });
    });
    if(!counter) promise.emitSuccess();
    });
    counter--;
    if (true === finished && 0 >= counter)
    callback(null);
    });
    }
    }

    walk('/home/node', function() {
    if (null === file)
    finished();
    });
    walk('/home/node', function() {});
  8. Tim-Smart revised this gist Feb 3, 2010. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -16,14 +16,14 @@ var listdir = function (pathname) {
    });
    return p;
    }


    var counter = 0;
    var walk = function (pathname, callback) {
    counter++;
    listdir(pathname).addCallback(function(files) {
    var finished = true;

    files.forEach(function(f) {
    var abspath = path.join(pathname, f);

    posix.stat(abspath).addCallback(function(stat) {
    if (stat.isDirectory()) {
    finished = false;
    @@ -33,12 +33,13 @@ var walk = function (pathname, callback) {
    }
    });
    });

    if (true === finished)
    counter--;
    if (true === finished && 0 >= counter)
    callback(null);
    });
    }

    walk('/home/node', function() {
    sys.puts(sys.inspect(arguments));
    if (null === file)
    finished();
    });
  9. Tim-Smart revised this gist Feb 3, 2010. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ var walk = function (pathname, callback) {
    posix.stat(abspath).addCallback(function(stat) {
    if (stat.isDirectory()) {
    finished = false;
    dirs.push(abspath);
    walk(abspath, callback);
    } else {
    callback(abspath);
    }
    @@ -39,7 +39,6 @@ var walk = function (pathname, callback) {
    });
    }

    walk('/home/node', function(file) {
    if (null === file)
    finished();
    });
    walk('/home/node', function() {
    sys.puts(sys.inspect(arguments));
    });
  10. Tim-Smart revised this gist Feb 3, 2010. 1 changed file with 21 additions and 9 deletions.
    30 changes: 21 additions & 9 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -3,31 +3,43 @@ var http = require('http'),
    path = require('path'),
    posix = require('posix'),
    events = require('events');

    var listdir = function (pathname) {
    var p = new events.Promise();
    var ls = process.createChildProcess("ls", [pathname]);
    ls.addListener("output", function (data) {
    if (data) {
    if (data) {
    var files = [];
    data.split('\n').forEach(function(f) {if (f){files.push(f)}});
    p.emitSuccess(files)
    }
    });
    return p;
    }

    var walk = function (pathname, callback) {
    listdir(pathname).addCallback(function(files) {
    var finished = true;

    files.forEach(function(f) {
    var abspath = path.join(pathname, f)
    var abspath = path.join(pathname, f);

    posix.stat(abspath).addCallback(function(stat) {
    if (stat.isDirectory()) {
    walk(abspath, callback);
    finished = false;
    dirs.push(abspath);
    } else {
    callback(abspath);
    }
    }).wait();
    })
    }).wait();
    }
    });
    });

    if (true === finished)
    callback(null);
    });
    }

    walk('/home/node', function(file) {
    if (null === file)
    finished();
    });
  11. @mikeal mikeal created this gist Feb 3, 2010.
    33 changes: 33 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    var http = require('http'),
    sys = require('sys'),
    path = require('path'),
    posix = require('posix'),
    events = require('events');

    var listdir = function (pathname) {
    var p = new events.Promise();
    var ls = process.createChildProcess("ls", [pathname]);
    ls.addListener("output", function (data) {
    if (data) {
    var files = [];
    data.split('\n').forEach(function(f) {if (f){files.push(f)}});
    p.emitSuccess(files)
    }
    });
    return p;
    }

    var walk = function (pathname, callback) {
    listdir(pathname).addCallback(function(files) {
    files.forEach(function(f) {
    var abspath = path.join(pathname, f)
    posix.stat(abspath).addCallback(function(stat) {
    if (stat.isDirectory()) {
    walk(abspath, callback);
    } else {
    callback(abspath);
    }
    }).wait();
    })
    }).wait();
    }