How to get directory size in node.js without recursively going through directory?

15,504

Solution 1

You can either spawn a du command on your target directory but as you said it can be rather slow the first time. What you might not know is that du results seem to be cached somehow:

$ time du -sh /var
13G /var
du -sh /var  0.21s user 0.66s system 9% cpu 8.930 total
$ time du -sh /var
13G /var
du -sh /var  0.11s user 0.34s system 98% cpu 0.464 total

It took initially 8s and then only 0.4s

Hence if your directories are not changing too often, just going with du might be the easiest way to go.

Another solution is to store that in a cache layer, so you can watch your root directory for changes, then compute the size of the folder, store it in a cache and just serve it when needed. To perform this you could use the watch functionality of NodeJS but you'll have some cross platform issues, hence a library like chokidar might be helpful.

Solution 2

fast-folder-size uses Sysinternals DU on Windows and the built-in du program on other platforms to quickly compute a folder size.

Installation

npm i fast-folder-size

Usage

const fastFolderSize = require('fast-folder-size')

fastFolderSize('.', (err, bytes) => {
  if (err) {
    throw err
  }

  console.log(bytes)
})

Solution 3

I use this simple async/await + fs Promises API (Node.js v14+) solution... It doesn't depend on external libraries or spawning new processes which is nice:

const { readdir, stat } = require('fs/promises');

const dirSize = async directory => {
  const files = await readdir( directory );
  const stats = files.map( file => stat( path.join( directory, file ) ) );

  return ( await Promise.all( stats ) ).reduce( ( accumulator, { size } ) => accumulator + size, 0 );
}

Usage:

const size = await dirSize( '/path/to/directory' );
console.log( size );

This doesn't use any loop constructs to recurse through the directory, although it is mapping/reducing arrays. The other solutions are just abstracting recursion behind NPM packages/C code so it should be all good...

Share:
15,504

Related videos on Youtube

user772401
Author by

user772401

Updated on June 21, 2022

Comments

  • user772401
    user772401 about 2 years

    How do I get the size of a directory in node.js without recursively going through all the children in a directory?

    E.g.

    var fs = require('fs');
    fs.statSync('path/to/dir');
    

    Will return me an object like this,

    { dev: 16777220,
      mode: 16877,
      nlink: 6,
      uid: 501,
      gid: 20,
      rdev: 0,
      blksize: 4096,
      ino: 62403939,
      size: 204,
      blocks: 0,
      atime: Mon May 25 2015 20:54:53 GMT-0400 (EDT),
      mtime: Mon May 25 2015 20:09:41 GMT-0400 (EDT),
      ctime: Mon May 25 2015 20:09:41 GMT-0400 (EDT) }
    

    But the size property is not the size of the directory and it's children (aka the sum of the files inside of it).

    Is there no way to get the size of a dir (w/the sizes of the files inside of it included) without recursively finding the sizes of the children (and then summing those up)?

    I'm basically trying to do the equivalent of du -ksh my-directory but if the given directory is really large (e.g /) than it takes forever to recursively get the true dir size..

    • user772401
      user772401 about 9 years
      I know that du -ksh / takes forever so maybe this question is ... mute... I'm hoping there's a linuxy thing I'm missing here..
    • Ry-
      Ry- about 9 years
      du does exactly that, so no, you can’t get around it.
    • jfriend00
      jfriend00 about 9 years
      As best I know, directories don't keep track of the accumulated size of all files below them so the only way to get the accumulated size is to recurse and add. It is not a fast operation.
  • Chait
    Chait about 7 years
    Hello, please expand your answer to include a solution that is useful even without the hyperlink. Thanks in advance.
  • Fomahaut
    Fomahaut over 6 years
    The module you post is using recursive solution. github.com/alessioalex/get-folder-size/blob/master/index.js#‌​L7
  • Radagast the Brown
    Radagast the Brown about 6 years
    may be OK for small shallow directories . Terrible for big deep directories. I'd rather run a shell command and let the OS handle it. It also does not give the size-on-disk - which is the common motive for checking folder size.
  • Kim T
    Kim T almost 4 years
    Even better you can get just the folder size in bytes using du -s /var | cut -f1
  • bluepuma77
    bluepuma77 about 2 years
    Where is "path" coming from?
  • Andrew Odri
    Andrew Odri about 2 years
    @bluepuma77 Hah, great question! It was undefined until I updated the answer after your your comment :P Thanks for catching that