Socket
Socket
Sign inDemoInstall

fast-glob

Package Overview
Dependencies
4
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-glob


Version published
Maintainers
1
Created

Package description

What is fast-glob?

The fast-glob package is a Node.js library that provides a fast and efficient way to match file paths against specified patterns. It uses the glob syntax, which is a way of filtering files in file systems using wildcard characters.

What are fast-glob's main functionalities?

Synchronous file searching

This feature allows you to perform synchronous file searches, returning an array of paths that match the specified patterns. The example code searches for all files except markdown files.

const fg = require('fast-glob');
const paths = fg.sync(['**/*', '!**/*.md']);

Asynchronous file searching

This feature allows you to perform asynchronous file searches, returning a promise that resolves with an array of paths that match the specified patterns. The example code searches for all files except markdown files and logs the result.

const fg = require('fast-glob');
fg.async(['**/*', '!**/*.md']).then(paths => {
  console.log(paths);
});

Stream interface for file searching

This feature provides a stream interface for file searching, emitting each matching path as a 'data' event. The example code searches for all files except markdown files and logs each matching path as it's found.

const fg = require('fast-glob');
const stream = fg.stream(['**/*', '!**/*.md']);
stream.on('data', (entry) => console.log(entry));

Other packages similar to fast-glob

Readme

Source

fast-glob

Is a faster (1.5-8x for most cases) node-glob alternative.

Install

$ npm i -S fast-glob

Why?

  • Fast by using Streams, Promises and Bash Globbing on Linux machines. Used readdir-enhanced, micromatch and bash-glob.
  • You can limit the depth of your search (only for non-Bash mode).
  • You can get not only file paths, but also their fs.Stats objects with the additional path property.
  • You can transform file path or fs.Stats object before sending it to an array.

Usage

const fastGlob = require('fast-glob');

// Async
fastGlob('dir/**/*.txt').then((files) => {
  console.log(files); // ['dir/a.txt', ...]
});

// Sync
const files = fastGlob.sync('dir/**/*.txt');
console.log(files); // ['dir/a.txt', ...]

API

fastGlob(patterns, [options])

  • patterns String|String[] Patterns to be matched
  • options Object
  • return String[] or fs.Stats[] with path property

fastGlob.sync(patterns, [options]) => []

  • patterns String|String[] Patterns to be matched
  • options Object
  • return String[] or fs.Stats[] with path property

options

OptionTypeDefaultDescription
cwdStringprocess.cwdThe current working directory in which to search
deep`NumberBoolean`true
ignore`StringString[]`[]
statsBooleanfalseReturn fs.Stats with path property instead of file path.
onlyFilesBooleanfalseReturn only files.
onlyDirsBooleanfalseReturn only directories.
bashNativeString[]['darwin', 'linux']Use bash-powered globbing (2-15x faster on Linux, but slow on BashOnWindows) for specified platforms. See available values for array.
transformFunctionnullAllows you to transform a path or fs.Stats object before sending to the array.

Compatible with node-glob?

Not fully, because fast-glob not implements all options of node-glob.

Example for transform option

fastGlob('dir/**/*.txt', { transform: readFilePromise }).then((files) => {
  console.log(files); // ['dir/a.txt', ...]
  return Promise.all(files);
}).then((files) => {
  console.log(files); // ['content from dir/a.txt', ...]
});

Benchmark

Tech specs:

  • Intel Core i7-3610QM
  • RAM 8GB
  • SSD (555MB/S, 530MB/S)
  • Windows 10 + VirtualBox with Manjaro
  • Node.js v7.3.0
$ npm run bench

==============================
Benchmark for 10 files
==============================

bash: 6 ms
node-glob (10): 19.47606 ms
bash-glob (10): 23.406374 ms
fast-glob (10) as native: 25.359293 ms
fast-glob (10) as fast: 33.696387 ms

==============================
Benchmark for 50 files
==============================

bash: 6 ms
node-glob (54): 28.43855 ms
bash-glob (54): 20.731202 ms
fast-glob (54) as native: 22.709236 ms
fast-glob (54) as fast: 25.057461 ms

==============================
Benchmark for 100 files
==============================

bash: 5 ms
node-glob (109): 34.811618 ms
bash-glob (109): 21.624256 ms
fast-glob (109) as native: 26.291311 ms
fast-glob (109) as fast: 29.61791 ms

==============================
Benchmark for 500 files
==============================

bash: 7 ms
node-glob (549): 90.513766 ms
bash-glob (549): 23.116938 ms
fast-glob (549) as native: 30.978642 ms
fast-glob (549) as fast: 61.367613 ms

==============================
Benchmark for 1000 files
==============================

bash: 11 ms
node-glob (1099): 139.44816 ms
bash-glob (1099): 35.691985 ms
fast-glob (1099) as native: 36.770455 ms
fast-glob (1099) as fast: 84.387065 ms

==============================
Benchmark for 5000 files
==============================

bash: 43 ms
node-glob (5499): 584.910373 ms
bash-glob (5499): 90.38803 ms
fast-glob (5499) as native: 97.143759 ms
fast-glob (5499) as fast: 233.977073 ms

==============================
Benchmark for 10000 files
==============================

bash: 96 ms
node-glob (10999): 1143.377267 ms
bash-glob (10999): 169.416486 ms
fast-glob (10999) as native: 198.930152 ms
fast-glob (10999) as fast: 531.693878 ms

Changelog

See the Releases section of our GitHub project for changelogs for each release version.

License

This software is released under the terms of the MIT license.

Keywords

FAQs

Last updated on 03 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc