Skip to content

Use modern JS features, ship TS defs#175

Merged
blakeembrey merged 2 commits into
masterfrom
be/typescript
Oct 7, 2024
Merged

Use modern JS features, ship TS defs#175
blakeembrey merged 2 commits into
masterfrom
be/typescript

Conversation

@blakeembrey
Copy link
Copy Markdown
Member

Refactored to modernize JS features used (const, let, etc) and bump the min node.js in prep for v1. Using TypeScript to ship native .d.ts files with the package moving forward. Removes some level of input validation relying on correct types (tiny perf win, less code checks).

@socket-security
Copy link
Copy Markdown

socket-security Bot commented Oct 4, 2024

Comment thread src/index.ts Outdated
str: string,
options?: ParseOptions,
): Record<string, string> {
const obj: Record<string, string> = {};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great PR!

I just have one feedback

Since this object is a map/record and can have any key, it would be beneficial and good practice to make its prototype null

Either by doing const obj = Object.create(null) or by using the more performant NullObject method (used in the Fastify router):

const NullObject = function () {}
NullObject.prototype = Object.create(null)

const obj = new NullObject();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Awesome, thanks!

I had tried Object.create(null) but found the perf impact was -10% or so, but NullObject is comparable or better than {}.

@blakeembrey blakeembrey force-pushed the be/typescript branch 2 times, most recently from 894787f to 585e23d Compare October 7, 2024 18:25
Comment thread src/index.ts Outdated
const __hasOwnProperty = Object.prototype.hasOwnProperty;

const NullObject: any = function () {};
NullObject.prototype = Object.create(null);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Small suggestion: This line is not treeshakable by esbuild playground

You might use something like this to make sure it is side-effect free.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in the latest commit. I'm unsure how much it helps overall since it's used in 50% of the exposed API, am I correct in understanding it's mostly for bundle size?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The package is currently still CommonJS, so that might also waste these bytes as it won't tree shake?

Copy link
Copy Markdown

@pi0 pi0 Oct 7, 2024

Choose a reason for hiding this comment

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

makes sense at least until being in CommonJS (not sure if it benefits rollup build with common js)

Comment thread package-lock.json
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a specific reason to add the lock file?

Copy link
Copy Markdown
Member Author

@blakeembrey blakeembrey Oct 7, 2024

Choose a reason for hiding this comment

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

Not particularly, I usually work with it checked it so the package versions are consistent and I can use npm ci. I'm not strongly opinionated either way, I suppose it's possible a dependency could change their behavior and break CI for a PR 🤷

Would you prefer to keep it removed?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't have a strong opinion for every package but we don't use them at Fastify -- they mostly don't achieve much for consumable packages

Feel free to do as you prefer, I was just curious to see if it was intentional, sometimes people mistakenly commit it and don't realize

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should not use them without automation which runs without the lock before each publish. The chance of end users seeing a breaking issue because we were accidentally locked back is not worth catching stray bugs during development due to updated transitive deps.

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.

4 participants