title: Why this Node stream.write not overwrite the file content? How the performance varies with Node fwrite?
body: <p>My intention is to write a file for particular interval. Created a node writeStream outside the interval and try to use the same stream inside the interval to write a content in a file. I expect the content to be overwritten but it's started to append. Here's the one approach</p>
  
  <p><strong>Approach I:</strong></p>
  
  <pre><code>var fs = require('fs');
  var ans = ["first","second","third","fourth","fifth"];
  var i =0;
  var stream = fs.createWriteStream('stats.log.info',{flags:'w'});
  var writeInterval = setInterval(function(){
    var data =ans[i%ans.length] ;
    if(data.length == 0){
      console.log("Failure in ");
    }
    stream.on('error', function(err){
      console.log("error in creating a stream "+err);
      clearInterval(writeInterval);
      stream.destroy();
      return;
    });
    stream.write(data);
  
    i++;
  },2000);
  </code></pre>
  
  <p>Then I moved created stream inside the interval which ends up in overwriting file content.
  <strong>Approach I:</strong>  created stream placed inside interval</p>
  
  <pre><code>var fs = require('fs');
  var ans = ["first","second","third","fourth","fifth"];
  var i =0;
  var writeInterval = setInterval(function(){
    var stream = fs.createWriteStream('stats.log.info',{flags:'w'});
  
    var data =ans[i%ans.length] ;
        if(data.length == 0){
          console.log("Failure in ");
        }
        stream.on('error', function(err){
          console.log("error in creating a stream "+err);
          clearInterval(writeInterval);
          stream.destroy();
          return;
        });
        stream.write(data);
  
        i++;
      },2000);
  </code></pre>
  
  <p>I am wondering how can I use one created stream to overwrite the file contents. I tried another approach using Node standard file write. It works perfectly to overwrite content. But it includes fopen and fclose inside interval which seems pretty expensive operations every time. Here's the sample</p>
  
  <p><strong>Approach II:</strong></p>
  
  <pre><code>var fs = require('fs');
  var ans = ["first","second","third","fourth","fifth"];
  var i =0;
  var writeInterval = setInterval(function(){
    var data =ans[i%ans.length] ;
    if(data.length == 0){
      console.log("Failure in ");
    }
      var fopen_write = fs.open("curr.log", 'w', function(err, fd){
          var buf = new Buffer(data, 'utf8');
          fs.write(fd, buf, 0, buf.length, null, function(err) {
              if (err) throw 'error writing file: ' + err;
              fs.close(fd, function() {
                  console.log('wrote the file successfully');
              });
          });
      })
    i++;
  },2000);
  </code></pre>
  
  <p>Suggestions in terms of how to evaluate the approaches with better performances are really helpful.</p>
  
  <p>I appreciate your time and efforts!     </p>