-
-
Notifications
You must be signed in to change notification settings - Fork 627
feat(form-core): 5-10x faster makePathArray
#2152
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
base: main
Are you sure you want to change the base?
Changes from all commits
ddc0f90
7ff53ba
ad169c0
f9b11af
ed8a902
015ea3a
51e125a
475b5c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/form-core': patch | ||
| --- | ||
|
|
||
| Improve performance for mounting/unmounting <form.Field> |
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I imagine this will be removed before this PR is merged (should that happen) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { bench, describe } from 'vitest' | ||
| import { makePathArray } from '../src/utils' | ||
|
|
||
| // Snapshot of the original implementation for side-by-side comparison. | ||
| // Remove this and the paired benches once the new implementation is merged. | ||
| const reLineOfOnlyDigits = /^(\d+)$/gm | ||
|
Comment on lines
+4
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
The inline 🤖 Prompt for AI Agents
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
(ノ◕ヮ◕)ノ*:・゚✧ |
||
| const reDigitsBetweenDots = /\.(\d+)(?=\.)/gm | ||
| const reStartWithDigitThenDot = /^(\d+)\./gm | ||
| const reDotWithDigitsToEnd = /\.(\d+$)/gm | ||
| const reMultipleDots = /\.{2,}/gm | ||
| const intPrefix = '__int__' | ||
| const intReplace = `${intPrefix}$1` | ||
|
|
||
| function makePathArrayOld( | ||
| str: string | Array<string | number>, | ||
| ): Array<string | number> { | ||
| if (Array.isArray(str)) { | ||
| return [...str] | ||
| } | ||
|
|
||
| if (typeof str !== 'string') { | ||
| throw new Error('Path must be a string.') | ||
| } | ||
|
|
||
| return str | ||
| .replace(/(^\[)|]/gm, '') | ||
| .replace(/\[/g, '.') | ||
| .replace(reLineOfOnlyDigits, intReplace) | ||
| .replace(reDigitsBetweenDots, `.${intReplace}.`) | ||
| .replace(reStartWithDigitThenDot, `${intReplace}.`) | ||
| .replace(reDotWithDigitsToEnd, `.${intReplace}`) | ||
| .replace(reMultipleDots, '.') | ||
| .split('.') | ||
| .map((d) => { | ||
| if (d.startsWith(intPrefix)) { | ||
| const numStr = d.substring(intPrefix.length) | ||
| const num = parseInt(numStr, 10) | ||
| if (String(num) === numStr) { | ||
| return num | ||
| } | ||
| return numStr | ||
| } | ||
| return d | ||
| }) | ||
| } | ||
|
|
||
| const cases: Array<[label: string, input: string | Array<string | number>]> = [ | ||
| ['array input (fast path, no parsing)', ['a', 'b', 0, 'c']], | ||
| ['simple key (no nesting)', 'key'], | ||
| ['uuid key', '550e8400-e29b-41d4-a716-446655440000'], | ||
| ['dot notation', 'foo.bar.baz'], | ||
| ['mixed dot and bracket notation', 'a[0].b[1]'], | ||
| ['deeply nested mixed path', 'a.b[0][1].c.d[2][3].e'], | ||
| ['numeric string with leading zeros (kept as string)', '01234'], | ||
| ['numeric string (converted to number)', '12345'], | ||
| ] | ||
|
|
||
| for (const [label, input] of cases) { | ||
| describe(label, () => { | ||
| bench('old', () => { | ||
| makePathArrayOld(input) | ||
| }) | ||
|
|
||
| bench('new', () => { | ||
| makePathArray(input) | ||
| }) | ||
| }) | ||
| } | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be dropped if we drop the benchmarks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be dropped if we drop the benchmarks