Skip to content

perf: stringify diff objects only once#10276

Merged
sheremet-va merged 7 commits into
vitest-dev:mainfrom
sheremet-va:perf/dont-stringify-multiple-times
May 7, 2026
Merged

perf: stringify diff objects only once#10276
sheremet-va merged 7 commits into
vitest-dev:mainfrom
sheremet-va:perf/dont-stringify-multiple-times

Conversation

@sheremet-va
Copy link
Copy Markdown
Member

@sheremet-va sheremet-va commented May 5, 2026

Description

Resolves vitest-dev/vscode#781

This PR does two things:

  • reuses the formatted string of expected/actual instead of creating a new one
    • this also makes it so the diff in other tools looks the same way because we do a special handling of async matchers
    • we follow diffOptions in those properties now (this causes the snapshot in tests to be big)

@netlify
Copy link
Copy Markdown

netlify Bot commented May 5, 2026

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit aa40bfe
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/69fa081120201100086c3bf1
😎 Deploy Preview https://deploy-preview-10276--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

plugins: PLUGINS,
} satisfies PrettyFormatOptions

export interface StringifiedMemory {
Copy link
Copy Markdown
Member Author

@sheremet-va sheremet-va May 5, 2026

Choose a reason for hiding this comment

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

I initially just wanted to use WeakMap, but we are cloning and reorganizing (set/map) objects, so it's easier to just have an object that sets it

Copy link
Copy Markdown
Collaborator

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

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

The idea makes sense to me, but is it important to stash the result through memorize?
Naively thinking, can we extend diff to return something like { result: string, actual?: string, expected?: string }? I'd get stashing could be simpler if diff is recursing beast, but doesn't look like so.
It could be just minor nit, but just asking.

Comment on lines +70 to +72
interface Memorize {
(pointer: 'expected' | 'actual', stringifiedValue: string): string
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah, I think what I felt odd is that memorize interface isn't even explicitly "stashing the result as argument" API.
So my question is, can this be diff(..., memory?: StringifiedMemory) directly instead of stashing through calling memorize: Memorize?

Copy link
Copy Markdown
Member Author

@sheremet-va sheremet-va May 7, 2026

Choose a reason for hiding this comment

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

It could, but it’s just less code with a util. It’s ugly to have val = string after every stringify call 😬

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I get the compactness but it feels an unnecessary indirection for my brain. not blocking though.

@sheremet-va
Copy link
Copy Markdown
Member Author

sheremet-va commented May 7, 2026

It could be just minor nit, but just asking.

We could return different result, but I think the approach in pr is just less code to implement, but it does rely a lot on mutating state which, I admit, is hard to follow

Comment on lines +70 to +72
interface Memorize {
(pointer: 'expected' | 'actual', stringifiedValue: string): string
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I get the compactness but it feels an unnecessary indirection for my brain. not blocking though.

@sheremet-va sheremet-va merged commit d006a68 into vitest-dev:main May 7, 2026
17 checks passed
@sheremet-va sheremet-va deleted the perf/dont-stringify-multiple-times branch May 7, 2026 14:46
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.

False Positive Annoyance

2 participants