Skip to content

Commit c89c66f

Browse files
replaceUndefinedAttributes 5x faster with caching (#4463)
(and probably comparatively even faster with more on-screen elements) Test page: [https://github.com/evnchn/nicegui-speed-investigation](https://github.com/evnchn/nicegui-speed-investigation) Tested on /native/after_conn/basic Before change: ![image](https://github.com/user-attachments/assets/b6992390-84bd-4edf-91ee-1a815bb0c726) After change: ![image](https://github.com/user-attachments/assets/a77279b5-198d-4782-a5b7-8821b232d531) For /native/after_conn/js, speed increase roughly lines up with #4457 (comment) I am lazy to take more screenshots 😅 --------- Co-authored-by: Falko Schindler <[email protected]>
1 parent 09c8b08 commit c89c66f

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

nicegui/static/nicegui.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ function parseElements(raw_elements) {
1919
);
2020
}
2121

22-
function replaceUndefinedAttributes(elements, id) {
23-
const element = elements[id];
24-
if (element === undefined) {
25-
return;
26-
}
22+
function replaceUndefinedAttributes(element) {
2723
element.class ??= [];
2824
element.style ??= {};
2925
element.props ??= {};
@@ -35,7 +31,6 @@ function replaceUndefinedAttributes(elements, id) {
3531
default: { ids: element.children || [] },
3632
...(element.slots ?? {}),
3733
};
38-
Object.values(element.slots).forEach((slot) => slot.ids.forEach((id) => replaceUndefinedAttributes(elements, id)));
3934
}
4035

4136
function getElement(id) {
@@ -318,7 +313,7 @@ window.onbeforeunload = function () {
318313
};
319314

320315
function createApp(elements, options) {
321-
replaceUndefinedAttributes(elements, 0);
316+
Object.entries(elements).forEach(([_, element]) => replaceUndefinedAttributes(element));
322317
setInterval(() => ack(), 3000);
323318
return (app = Vue.createApp({
324319
data() {
@@ -381,16 +376,15 @@ function createApp(elements, options) {
381376
const loadPromises = Object.entries(msg)
382377
.filter(([_, element]) => element && (element.component || element.libraries))
383378
.map(([_, element]) => loadDependencies(element, options.prefix, options.version));
384-
385379
await Promise.all(loadPromises);
386380

387381
for (const [id, element] of Object.entries(msg)) {
388382
if (element === null) {
389383
delete this.elements[id];
390384
continue;
391385
}
386+
replaceUndefinedAttributes(element);
392387
this.elements[id] = element;
393-
replaceUndefinedAttributes(this.elements, id);
394388
}
395389
},
396390
run_javascript: (msg) => runJavascript(msg.code, msg.request_id),

0 commit comments

Comments
 (0)