Skip to content

Commit 75a9e9d

Browse files
Merge pull request #798 from preactjs/signal-perf
fix(perf): only check for devtools once
2 parents 154b591 + e58734d commit 75a9e9d

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

.changeset/giant-falcons-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@preact/signals": patch
3+
---
4+
5+
Fix performance regression by checking if the `@preact/signals-debug` package is enabled only once.

packages/preact/src/index.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export {
3131
untracked,
3232
};
3333

34+
const DEVTOOLS_ENABLED =
35+
typeof window !== "undefined" && !!window.__PREACT_SIGNALS_DEVTOOLS__;
36+
3437
const HAS_PENDING_UPDATE = 1 << 0;
3538
const HAS_HOOK_STATE = 1 << 1;
3639
const HAS_COMPUTEDS = 1 << 2;
@@ -172,11 +175,7 @@ Object.defineProperties(Signal.prototype, {
172175

173176
/** Inject low-level property/attribute bindings for Signals into Preact's diff */
174177
hook(OptionsTypes.DIFF, (old, vnode) => {
175-
if (
176-
typeof vnode.type === "function" &&
177-
typeof window !== "undefined" &&
178-
window.__PREACT_SIGNALS_DEVTOOLS__
179-
) {
178+
if (DEVTOOLS_ENABLED && typeof vnode.type === "function") {
180179
window.__PREACT_SIGNALS_DEVTOOLS__.exitComponent();
181180
}
182181

@@ -201,11 +200,7 @@ hook(OptionsTypes.DIFF, (old, vnode) => {
201200

202201
/** Set up Updater before rendering a component */
203202
hook(OptionsTypes.RENDER, (old, vnode) => {
204-
if (
205-
typeof vnode.type === "function" &&
206-
typeof window !== "undefined" &&
207-
window.__PREACT_SIGNALS_DEVTOOLS__
208-
) {
203+
if (DEVTOOLS_ENABLED && typeof vnode.type === "function") {
209204
window.__PREACT_SIGNALS_DEVTOOLS__.enterComponent(vnode);
210205
}
211206

@@ -237,7 +232,7 @@ hook(OptionsTypes.RENDER, (old, vnode) => {
237232

238233
/** Finish current updater if a component errors */
239234
hook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {
240-
if (typeof window !== "undefined" && window.__PREACT_SIGNALS_DEVTOOLS__) {
235+
if (DEVTOOLS_ENABLED) {
241236
window.__PREACT_SIGNALS_DEVTOOLS__.exitComponent();
242237
}
243238

@@ -248,11 +243,7 @@ hook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {
248243

249244
/** Finish current updater after rendering any VNode */
250245
hook(OptionsTypes.DIFFED, (old, vnode) => {
251-
if (
252-
typeof vnode.type === "function" &&
253-
typeof window !== "undefined" &&
254-
window.__PREACT_SIGNALS_DEVTOOLS__
255-
) {
246+
if (DEVTOOLS_ENABLED && typeof vnode.type === "function") {
256247
window.__PREACT_SIGNALS_DEVTOOLS__.exitComponent();
257248
}
258249

0 commit comments

Comments
 (0)