Skip to content

Commit

Permalink
feat(transform): replace object spread with Proxy in getTagProcessor (#…
Browse files Browse the repository at this point in the history
…57)

* feat(transform): replace object spread with Proxy in getTagProcessor

* fix(transform): revert babel.compact
  • Loading branch information
Anber authored Feb 9, 2024
1 parent c6c5801 commit 3a494ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-stingrays-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@wyw-in-js/transform': patch
---

Found out that an object spread can be extremely slow. getTagProcessor now works 10 times faster.
15 changes: 13 additions & 2 deletions packages/transform/src/utils/getTagProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ function getBuilderForIdentifier(
});
};

const astService = {
...t,
const importHelpers = {
addDefaultImport: (importedSource: string, nameHint?: string) =>
addDefault(path, importedSource, { nameHint }),
addNamedImport: (
Expand All @@ -326,6 +325,18 @@ function getBuilderForIdentifier(
) => addNamed(path, name, importedSource, { nameHint }),
};

type AstService = typeof t & typeof importHelpers;

const astService = new Proxy<AstService>(t as AstService, {
get(target, prop, receiver) {
if (prop in importHelpers) {
return importHelpers[prop as keyof typeof importHelpers];
}

return Reflect.get(target, prop, receiver);
},
});

return (...args: BuilderArgs) =>
new Processor(
params,
Expand Down

0 comments on commit 3a494ef

Please sign in to comment.