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

perf: use fast property access instead of get were possible #11389

Merged
merged 1 commit into from
Feb 15, 2022

Conversation

Uzlopak
Copy link
Collaborator

@Uzlopak Uzlopak commented Feb 13, 2022

With this PR I replaced the get-method with fast property access were it is possible. I know that it looks not so sexy like the get call, but it has performance improvements as we dont need to split the path, traverse an array and stuff.

before:
$ MONGOOSE_DEV=1 node benchmarks/clone.js
Simple x 140,319 ops/sec ±1.35% (92 runs sampled)
AllSchema x 23,986 ops/sec ±1.26% (90 runs sampled)

MONGOOSE_DEV=1 node benchmarks/validate.js
invalid x 22,726 ops/sec ±0.62% (95 runs sampled)
valid x 61,786 ops/sec ±0.71% (93 runs sampled)

after
$ MONGOOSE_DEV=1 node benchmarks/clone.js
Simple x 196,277 ops/sec ±0.72% (92 runs sampled)
AllSchema x 28,769 ops/sec ±0.41% (94 runs sampled)

$ MONGOOSE_DEV=1 node benchmarks/validate.js
invalid x 23,488 ops/sec ±2.98% (95 runs sampled)
valid x 69,983 ops/sec ±1.05% (92 runs sampled

So atleast cloning is about 25 % faster.

We need more benchmarks imho... but hey. its something

Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

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

Looks great, thanks 👍
Can't wait till we deprecate node >14 and use optional chaining inside mongoose.

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Thanks, I'll merge this since this improves perf on code paths that are commonly used.

However, while we can't use optional chaining yet, I'd love to see if there's a way we could improve get()'s performance so we don't need as many && checks. One thing we did to improve performance was accept array syntax, so you could do get(_schematype, ['schema', 'options', 'discriminatorKey']) and skip the perf overhead of split(). Does that help perf or is using && still meaningfully faster?

@vkarpov15 vkarpov15 added this to the 6.2.2 milestone Feb 15, 2022
@vkarpov15 vkarpov15 merged commit 426fac2 into Automattic:master Feb 15, 2022
@vkarpov15 vkarpov15 mentioned this pull request Oct 1, 2023
1 task
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.

None yet

3 participants