Skip to content

Instantly share code, notes, and snippets.

@jonschlinkert
Last active July 9, 2019 20:04
Show Gist options
  • Save jonschlinkert/96212e11d128fc659d12 to your computer and use it in GitHub Desktop.
Save jonschlinkert/96212e11d128fc659d12 to your computer and use it in GitHub Desktop.

micromatch vs. minimatch (WIP)

Can micromatch be used as a drop-in replacement for minimatch?

For mainstream features, I tried to keep as much parity as possible between micromatch and minimatch. But there are some differences.

Key differences

  • the main minimatch function, minimatch(), works like micromatch.isMatch()
  • the main micromatch function, micromatch(), works like multimatch, with support for multiple patterns.
  • micromatch optimizes patterns to generate the leanest possible regex to use for matching without sacrificing accuracy.

Optimized regular expressions

Micromatch's optimizations are achieved in a number of different ways.

Brace expansion

It's not uncommon to do this in a gulp or Grunt task:

src('*.{yml,json}');

Features

feature micromatch minimatch notes
multiple patterns yes no ex: ['*.js', '!foo']
# comments in file paths no yes
brace expansion yes yes ex: *.{txt,md}
regex character classes yes sort of ex: [a-c]*.js, match file names starting with a through c
extglobs yes yes ex: `+(foo
POSIX bracket expressions yes no (character classes) ex: [[:alpha:][:lower:]]
regex or string yes no Micromatch will take a regex or a glob pattern to use for matching.

multiple pattern support

Support for matching against multiple patterns, like ['*.js', '!foo']:

  • Minimatch: no
  • Micromatch: yes

Because of this, there is also a key difference in how the main exported function from each library is used.

Key difference

  • micromatch(): the main micromatch() function works like multimatch, and supports matching with multiple patterns (e.g. ['*.js', '!foo']).
  • minimatch(): the main minimatch() function works like micromatch.isMatch(), returning true if a single path matches the given pattern.

API

Methods

method micromatch minimatch notes
matchOne no yes like match, but only the first file
makeRe yes yes create a regular expression from the pattern.
match yes yes return an array of matches from a single pattern
filter yes yes like match but returns a function that can be passed to Array.filter
contains yes no like match, but matches any part of a path, not just the entire path
expand yes no returns an object of tokens, which are passed to .makeRe()
matcher yes no returns a function to use for matching
isMatch yes no returns true if a path matches the given pattern. Works like minimatch()
matchKeys yes no match the keys in an object

Options

option micromatch minimatch description
flipNegate no yes
failglob yes no throw when no matches are found (bash parity)
ignore yes no string or array of patterns to ignore. like negate, but passed on options.
nocase yes yes ...
nonull yes yes ...
nullglob yes yes ...
nonegate yes yes ...

Other differences

benchmarks

micromatch is faster in every benchmark by a significant margin. One significant case that stands out is matching on arrays with thousands of items (like filepaths in a project). Here are the results from the benchmarks for matching against an array of ~7-8k items:

micromatch.js x 773 ops/sec ±0.62% (98 runs sampled)
minimatch.js x 27.52 ops/sec ±0.66% (49 runs sampled)

Bash 4.3

micromatch has better Bash 4.3 coverage along with extensive, organized unit tests

micromatch isn't a constructor

However, if you're using new Minimatch() because you need to do some kind of customization to the pre-regex pattern or whatever, then you should be able to achieve the same or similar results with micromatch.

In particular, micromatch.expand() parses the glob pattern and returns an object. You can then pass that to the .makeRe() method to generate the regex for matching.

Notes

(nothing yet)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment