gulp stream.push

18 views
Skip to first unread message

Георгий Катерняк

unread,
Jul 26, 2016, 9:38:16 AM7/26/16
to nodejs
#gulp #plugin.
Hi ) I'm not a javascript guru, so ...
I've already made the task, but have problems with the speed
I stacked with stream.Transform.push. Here is test code


var fs = require("fs"),
    through2
= require("through2"),
   
Transform = require("stream").Transform,
    vinyl
= require("vinyl"),
    async
= require("async");

.
.
.
resolveDependant
= function(file, stream, cb){

         
/**
         * @var array tree - list of files[], aka driectory tree
         */

       
var reg = new RegExp("Catch me!", "gm"),//ignore
            seriesList
= []; //for threading

       
for(var f in tree){

            seriesList
.push( //ignore
               
function(file){ //ignore
                   
return function(callback){ //ignore
                        fs
.readFile(file, { encoding : "utf8" }, function(err, data){ //ignore
                           
if(reg.exec(data)){ //trying to catch him!
                                stream
.push( //here is time leak! For 4 files it spent 1.6s.
                                   
new vinyl({  //without previous line it spent 0.4s for same 4 files
                                        path
: file,
                                        contents
:
                                           
new Buffer(data)
                                   
})
                               
);
                           
}
                            callback
();
                       
});
                   
}
               
}(tree[f])
           
);
       
}

.
.
.
       
this.chain = function() { //gulp.pipe(obj.chain()) call
       
return through2.obj(function (file, enconding, cb) {
         
/**
         * @var array tree - list of files[], aka driectory tree
         */

           
if(!tree.length) //just ensure that chain was called after initialization
               
return cb(null, file);

           
var stream = this;

            resolveDependant
(file.path, stream, function(){
                stream
.push(file); //test mode ignore any input
                cb
(); //return control to pipe
           
});
       
});
   
}



Can anyone explain me why it spent so much time to push vinyl files into stream
stream.push( ... );


Any help, books, advices and etc. will be helpfull.
Thanks
Reply all
Reply to author
Forward
0 new messages