npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fstorm

v0.1.3

Published

writing file more efficient and safety with no pain

Downloads

1,568

Readme

fstorm Build Status

writing file like a storm , but always get the expected result

A very very small idea for writing file more 'safe' and 'super fast', inspired by steno, but more efficient and sensible.

fstorm is very suitable to be used when you need to excute frequently writing to same file.

What fstorm do?

Take writeFileSync, writeFile, steno for comparison.

You can also find this benchmark in benchmark folder

RESULT:

10000x

image

100000x

image

1000000x

image

steno will pending without any response, the same as writeFile and writeFileSync

About writeFile

In steno's page. It mentioned that writeFile only take 20ms to writeFile, It is compeletely wrong. In fact, when you run fs.writeFile(..), It is only represent that the code is over, but file haven't been compelete yet. Althought not fast as expected, but it is still much faster than fs.writeFileSync

About steno

steno's setCallback and write(content, callback) are both meanless for hook the content after writting.

About writeFileSync

It is really sloooooooooooooooow..., but it is really reliable, beacuse it execute every file writing one by one..

fstorm!!

Now, There is a more reasonable choice when you need to writing file frequently, only the last one will be kept, and it is dead fast beacuse some trick to avoid unnecessary operation.

source code

var benchmarks = {
  "writeFileSync": function(time, cb){

    var filename = f("writeFileSync.txt");
    var k = 0;

    function compelete(){
      k++
      if( (k >= time) && cb) cb()
    }
    for (var i = 0; i < time; i++) {
      fs.writeFileSync(filename, i)
      compelete()
    }
  },
  "writeFile": function(time, cb){
    var filename = f("writeFile.txt");
    var k = 0;
    function compelete(){
      k++;
      if( (k >= time) && cb) cb()
    }
    for (var i = 0; i < time; i++) {
      fs.writeFile( filename , i, compelete)
    }

  },
  // I try setCallback for steno, it is also fail to get correct content.
  "steno": function(time, cb){
    var filename = f("steno.txt");
    var sfile = steno(filename);
    var k =0;

    function compelete(){
      k++;
      if( (k >= time) && cb) cb()
    }
    for (var i = 0; i < time; i++) {
      sfile.write(i, compelete)
    }
  },

  "fstorm": function( time, cb ){

    var filename = f("fstorm.txt");
    var k =0;
    var fwriter = fstorm(filename);

    fwriter.on('end', cb) // emitted when a squence is over

    for (var i = 0; i < time; i++) {
      fwriter.write(i)
    }
  }

}

Usage

npm install fstorm
var fstorm  = require('fstorm');

var writer = fstorm('./file3.txt');

writer.on('end', function(){
  assert(fs.readFileSync('./file3.txt') === '7')
})

writer
  .write('1')
  .write('2')
  .write('3')
  .write('4')
  .write('5')
  .write('6')

process.nextTick(function(){
  writer
    .write('7')
})

API

fstorm(filename)

return a writer instance

  • filename: the dest file's name
var writer = fstorm(filename);

writer.write( content[, options] [, callback])

  • content: the content you want to write
  • options[Optional]: fstorm use fs.writeFile(filename, options, callback). the options will be passed to it. default is 'utf8'
  • callback(err, status):
    • err: follow the 'node-callback-style', if any error is occurred, it will be return.
    • status: if status is 0, mean that this operation will be ignored beacuse of following writing operations . if status is 1, the content has been written successfully.

Example


writer.write('1', function(err, status){
  console.log(status)// ===> 0  
})

writer.write('2', function(err, status){
  console.log(status)// ===> 1  
})

builtin event

__FstormWriter is a SubClass of EventEmitter. __

Temporary, only end and error is emitted by writer, mean that writer is stable (or no new operation is blocked).

  • end

writer.on('end', function(content){
  console.log(content === '2') // true
})

writer.write('1')

writer.write('2')
  • error

var writer = fstorm('.folder/not/exists/db.json')

writer.on('error', function(err){
  assert(err.code === 'ENOENT')  // true
})

writer.write('2')

Contribution

benchmark

npm run benchmark

test

npm test

License

MITTTTTTTTTTTTTTTTTT.