Skip to content

refactor(internals): reduce flushPending overhead (performance)#3285

Open
dmaskasky wants to merge 1 commit intopmndrs:mainfrom
dmaskasky:refactor-flush-pending
Open

refactor(internals): reduce flushPending overhead (performance)#3285
dmaskasky wants to merge 1 commit intopmndrs:mainfrom
dmaskasky:refactor-flush-pending

Conversation

@dmaskasky
Copy link
Copy Markdown
Collaborator

Summary

  • Adds an early return when there is no work to do
  • Replaces forEach with for..of loops - slightly better performance?

Check List

  • pnpm run fix for formatting and linting code and docs

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 30, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
jotai Ready Ready Preview, Comment Apr 3, 2026 6:09am

Request Review

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci bot commented Mar 30, 2026

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@dmaskasky
Copy link
Copy Markdown
Collaborator Author

FlushPending isn't all that hot. We can close this PR if we don't like the readability.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 30, 2026

More templates

npm i https://pkg.pr.new/jotai@3285

commit: c29f90f

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 30, 2026

LiveCodes Preview in LiveCodes

Latest commit: c29f90f
Last updated: Apr 3, 2026 6:09am (UTC)

Playground Link
React demo https://livecodes.io?x=id/DARQFBG5R

See documentations for usage instructions.

@dmaskasky dmaskasky changed the title Refactor flush pending refactor flushPending Mar 30, 2026
@dmaskasky dmaskasky force-pushed the refactor-flush-pending branch from c46f161 to b30df9d Compare April 1, 2026 17:57
Comment on lines +401 to +408
if (
changedAtoms.size === 0 &&
mountCallbacks.size === 0 &&
unmountCallbacks.size === 0 &&
!storeHooks.f
) {
return
}
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.

I think changing do-while loop to while loop should be more readable and improve the performance for most cases. Only the concern is the store hook, right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah. Also, btw flushCallbacks is not a hot path. Let me see if I can switch to a while.

Copy link
Copy Markdown
Collaborator Author

@dmaskasky dmaskasky Apr 3, 2026

Choose a reason for hiding this comment

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

its ugly. I think I prefer the code as-written.

...
if (storeHooks.f) {
  call(storeHooks.f)
}
let iterations = 0
while (changedAtoms.size || unmountCallbacks.size || mountCallbacks.size) {
  if (storeHooks.f && iterations++ > 0) {
    call(storeHooks.f)
  }
  ...
}

@dmaskasky dmaskasky force-pushed the refactor-flush-pending branch from 1fd8c9e to c29f90f Compare April 3, 2026 06:09
@dmaskasky dmaskasky changed the title refactor flushPending refactor(flushCallback): reduce callback pressure and add early return Apr 3, 2026
@dmaskasky dmaskasky changed the title refactor(flushCallback): reduce callback pressure and add early return refactor(internals): reduce flushPending overhead (performance) Apr 3, 2026
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.

2 participants