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

Use modern JS features, ship TS defs #175

Merged
merged 2 commits into from
Oct 7, 2024
Merged

Use modern JS features, ship TS defs #175

merged 2 commits into from
Oct 7, 2024

Conversation

blakeembrey
Copy link
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).

Copy link

socket-security bot commented Oct 4, 2024

src/index.ts Outdated
str: string,
options?: ParseOptions,
): Record<string, string> {
const obj: Record<string, string> = {};
Copy link
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
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
src/index.ts Outdated
const __hasOwnProperty = Object.prototype.hasOwnProperty;

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

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
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
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

@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)

Copy link
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
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
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

@blakeembrey blakeembrey marked this pull request as ready for review October 7, 2024 21:55
@blakeembrey blakeembrey merged commit 1cc64ff into master Oct 7, 2024
8 checks passed
@blakeembrey blakeembrey deleted the be/typescript branch October 7, 2024 22:17
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.

3 participants