Refactor to faster API shape - #103
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #103 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 1 1
Lines 64 112 +48
Branches 31 49 +18
=========================================
+ Hits 64 112 +48 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Perf before: Perf after: The new APIs are much faster, but I've tried my best that even if you use the new API is the one shot approach to have comparable performance. E.g. compare |
|
The only thing I didn't tackle from the plan in #96 was the class approach. I will do a follow up with that, it would be better for re-use since |
|
|
||
| for (const t of types) { | ||
| const contentType = parse(t); | ||
| hasParameters ||= Object.keys(contentType.parameters).length > 0; |
There was a problem hiding this comment.
Perf de-optimization only when a potential match has requested parameters to be matched.
|
|
||
| return false; | ||
| export interface NormalizeOptions { | ||
| extensions?: Record<string, string | string[]>; |
There was a problem hiding this comment.
It probably makes more sense to be a function to support https://www.npmjs.com/package/mime and others that may not expose in object format.
|
The last commit replaces the functional style with a class. I'm indifferent on which direction is best, they both have up/downsides. The class might be helpful if you want to parse once and use in multiple places. The functional is a bit more verbose but helps with code splitting, e.g. in browsers, where you only use |
bjohansebas
left a comment
There was a problem hiding this comment.
LGTM, it took me a little while to understand, but the changes look good to me
Refactors the API to be a cached/preprocessed function closure instead of parsing the matching pattern every time. Additionally solves #14 by parsing parameters and matching them when provided.
It also changes the returned type to always be the matched one, instead of returning the matched except when it contained a wildcard.
Drops usage of
mime-typesfrom the library so it can be more easily used in a browser (the dependency is huge): #45. However, I kept an option forextensionsso it's possible formime-typesto still be passed into the library upstream (e.g. in express) without largely breaking expectations if wanted.