Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace minimatch with the faster alternative picomatch #1374

Merged
merged 1 commit into from
Mar 8, 2024

Conversation

Torathion
Copy link
Contributor

Replaces minimatch with the smaller (434 KB -> 85 KB) and faster alternative picomatch.

Picomatch is also the faster alternative to micromatch and nanomatch that aren't maintained anymore. Picomatch uses the factory pattern to create Matchers to be used to match strings:

import pm from 'picomatch'

const matcher = pm('*.js')

console.log(matcher('a.js')) // true
console.log(matcher('b.ts')) // false

Written inline, it looks like this:

import picomatch from 'picomatch'

console.log(picomatch('*.js')('a.js')) // true

Some other things:

  • You should refactor the composeFilter function, because every inline function, such as predicate, matchUnscoped and matchScoped are completely rebuild every time you call composeFilter. This pattern is also the reason why nested forEach-loops are incredibly slow, due to the constant function rebuilding.
  • I replaced filterPattern[filterPattern.length -1] with filterPattern.at(-1) which is a new prototype function that is twice as fast. You should integrate it into your project or use eslint-plugin-unicorn to fix all occurrences of it.

@Torathion Torathion changed the title Replace minimatch with the faster alternative picomatch. Replace minimatch with the faster alternative picomatch Mar 8, 2024
Copy link
Owner

@raineorshine raineorshine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, a nice improvement. I didn't know about .at.

I'll keep the composeFilter function optimization in mind as a future area of improvement. I would need to think about how to refactor it in a way that preserves some of the elegance of the higher order functions.

@raineorshine raineorshine merged commit ab0aff0 into raineorshine:main Mar 8, 2024
10 checks passed
@Torathion Torathion deleted the replace-minimatch branch March 8, 2024 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants