Nerdiest Precious Modules

    nbfs

    0.2.1 • Public • Published

    nbfs

    NonBlocking ~Nodejs~ File System

    Why?

    When running an application in NodeJS, it’s single threaded, and will only utilise a single core.

    When performing cpu-intensive or a great number of tasks, you may see this impacting performance, and see runtime/respond-time increase.

    If your NodeJS Application has 100% cpu-usage and is taking a long time to complete or slow to respond, this can be improved by dividing the work to be done, and spreading it over multiple processes.

    Traditional (Many tasks to a single node process)


    Traditional

    Distributed (Many tasks to distributed to multiple worker processes)


    Distributed

    nbfs creates and manage multiples processes which communicate between themself. This approach helps a lot for a non-blocking nodejs architechure.

    Even if you use FS native stream API or based on async ways to this job, will always run on the nodejs main thread (in idle status or not).

    Read more about it

    Install

    For install nbfs, just run in your terminal:

    npm i nbfs -S

    Streams

    read

    const { read } = require('nbfs')
    const stream = read('./my-file.js') // absolute path
     
    stream.on('read', (content) => {
      console.log(content) // 'abc'
    })
     
    stream.on('end', (result) => {
      console.log(result) // {path: './my-file.js', content: 'abc', operation: 'read'}
    })
     
    stream.on('error', (error) => {
      console.log(error)
    })

    write

    const { write } = require('nbfs')
    const stream = write('./my-file.js', 'hello-world') // absolute path
     
    stream.on('write', (content) => {
      console.log(content) // 'hello-world'
    })
     
    stream.on('end', (result) => {
      console.log(result) // {path: './my-file.js', content: 'hello-world', operation: 'write'}
    })
     
    stream.on('error', (error) => {
      console.log(error)
    })

    Benchmark

    fs.readFile x 10,738 ops/sec ±2.46% (77 runs sampled)
    nbfs.read x 7,701 ops/sec ±2.74% (75 runs sampled)
    fs.readFileSync x 49,473 ops/sec ±1.75% (42 runs sampled)
    exec cat x 148 ops/sec ±1.57% (43 runs sampled)

    list

    isDirectory

    folderPath

    Install

    npm i nbfs

    DownloadsWeekly Downloads

    2

    Version

    0.2.1

    License

    MIT

    Last publish

    Collaborators

    • raphamorim