I wonder if it slow because of the locking, because other than that it shouldn't use much resources to run this.
Anyway I am lazy, so I would probably just use HTMX boosting:
<div hx-boost="true">
// traditional code goes here
</div>