[LiveComponent] Skip the bracket pipeline for plain model names - #3790
Closed
Kocal wants to merge 1 commit into
Closed
[LiveComponent] Skip the bracket pipeline for plain model names#3790Kocal wants to merge 1 commit into
Kocal wants to merge 1 commit into
Conversation
| Q | A
| -------------- | ---
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Documentation? | no
| Issues | -
| License | MIT
`normalizeModelName()` ran a `replace()`, a `split()`, a `map()` with a
second `replace()` per part and a `join()` on every call, even for a plain
name like `query` where the whole pipeline is a no-op.
It runs on every input event through `getValueFromElement()`, and twice per
`ValueStore.set()` since `set()` normalizes and then calls `get()`, which
normalizes again.
Return the name untouched when it contains neither `[` nor `]`. Names that
do use the bracket syntax take the same path as before.
400k calls: plain names ~45 ms -> ~12 ms, a realistic 80/20 mix ~43 ms ->
~17 ms, bracketed names unchanged (~84 ms). These are micro-seconds per
event in absolute terms, but the change is three lines and output is
identical.
Browser-side JavaScript, so there is no Blackfire profile for this one.
Benchmarked on node 22 with `node bench.mjs`, comparing both
implementations side by side:
```js
function current(model) {
return model.replace(/\[]$/, '').split('[').map((s) => s.replace(']', '')).join('.');
}
function candidate(model) {
if (!model.includes('[') && !model.includes(']')) {
return model;
}
return model.replace(/\[]$/, '').split('[').map((s) => s.replace(']', '')).join('.');
}
const plain = ['firstName', 'email', 'query', 'isEnabled', 'selectedId'];
const bracketed = ['user[firstName]', 'user[mailing][]', 'form[items][0][label]'];
for (const [label, names] of [
['plain only', plain],
['bracketed only', bracketed],
['mixed 80/20', [...plain, ...plain, ...plain, ...plain, ...bracketed]],
]) {
for (const [impl, fn] of [['current', current], ['candidate', candidate]]) {
for (const n of names) fn(n); // warm up
const start = process.hrtime.bigint();
for (let i = 0; i < 400000; i++) {
fn(names[i % names.length]);
}
console.log(`${label} ${impl} -> ${(Number(process.hrtime.bigint() - start) / 1e6).toFixed(2)} ms`);
}
}
```
A stray `]` with no `[` was the one input where the fast path could have
diverged, so it is now covered by a unit test.
Analysis, implementation and benchmarks by Claude Opus 5.
Member
Author
|
Duplicate of #3786, same branch. |
Contributor
📊 Packages dist files size differenceThanks for the PR! Here is the difference in size of the packages dist files between the base branch and the PR.
|
|||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
normalizeModelName()ran areplace(), asplit(), amap()with asecond
replace()per part and ajoin()on every call, even for a plainname like
querywhere the whole pipeline is a no-op.It runs on every input event through
getValueFromElement(), and twice perValueStore.set()sinceset()normalizes and then callsget(), whichnormalizes again.
Return the name untouched when it contains neither
[nor]. Names thatdo use the bracket syntax take the same path as before.
400k calls: plain names ~45 ms -> ~12 ms, a realistic 80/20 mix ~43 ms ->
~17 ms, bracketed names unchanged (~84 ms). These are micro-seconds per
event in absolute terms, but the change is three lines and output is
identical.
Browser-side JavaScript, so there is no Blackfire profile for this one.
Benchmarked on node 22 with
node bench.mjs, comparing bothimplementations side by side:
A stray
]with no[was the one input where the fast path could havediverged, so it is now covered by a unit test.
Analysis, implementation and benchmarks by Claude Opus 5.