When your browser game grows to 144 handcrafted 3D islands, 111 towers & generators, and 534 Blender-authored PBR GLB models, "it runs fine on my dev machine" stops being a valid performance metric.
Earlier this week, a real player test on POWER TD: Grid Defense (a free Three.js / WebGL open-field mazing + electrical-grid tower defense) gave us the clearest possible feedback: "It still feels choppy during combat and takes too long to load."
Instead of guessing, we instrumented cold startup, the 3D Island Atlas preview, and 1×/4× speed combat in Playwright with hardware Metal WebGL (2× DPR) — and uncovered five non-obvious bottlenecks that had nothing to do with polygon counts.
Here are the exact before/after numbers and the five fixes that cut initial bytes by 33.8%, boosted 3D preview FPS by +46.1%, and dropped >8 ms main-thread combat frame spikes to zero.
Real Before → After Benchmarks
Measured on identical cold-start & combat scenarios (Playwright hardware WebGL, 2× DPR, EN + ZH-Hant locales):
| Metric | Before | After | Delta |
|---|---|---|---|
Atlas visible (t_atlas_visible_ms) |
613.7 ms |
277.4 ms |
-54.8% |
| Cold click Play → first 3D paint | 1,038.4 ms |
883.2 ms |
-14.9% |
| Requests before first battle paint | 273 |
159 |
-41.8% |
| Bytes transferred before first battle paint | 10.35 MB |
6.86 MB |
-33.8% |
| 3D Island Atlas preview FPS | 52.9 FPS |
77.3 FPS |
+46.1% |
| Atlas offscreen FBO passes / frame | 33.5 |
0.5 |
-98.5% |
| Atlas draw calls / frame | 313.0 |
173.5 |
-44.6% |
| Combat 1× FPS / draw calls | 68.0 FPS / 193.9 |
76.3 FPS / 160.0 |
+12.2% FPS |
| Combat 4× FPS / max frame time | 69.1 FPS / 36.8 ms |
73.7 FPS / 26.7 ms |
0 frames > 33ms |
EN combat main-thread rAF CPU (avg / p95 / max) |
4.16 / 17.2 / 21.2 ms |
2.39 / 4.4 / 7.7 ms |
0 spikes > 8ms |
| Console warnings / errors | 12 |
0 |
-100% |
Bottleneck 1: Eager Model & Texture Requests Flooding Cold Boot
Even though our 534 GLB models were already Meshopt + WebP compressed and split by island, two startup leaks were firing 273 network requests (10.35 MB) before the first combat frame painted:
- The 3D Atlas Preview (
atlasPreview.ts) was eagerly requesting showcase & adjacent-biome models and running the full post-processing pipeline on the landing screen. -
Pre-hydration click loss: When a fast player clicked Play in the first 300ms before the locale chunk (
locale-en-*.js) finished hydrating, the button click was silently dropped, making startup feel sluggish even when assets were fast.
The Fix
- Scoped initial preloads strictly to the active island's towers, terrain, and wave-1 enemies, deferring non-visible showcase props until idle.
- Added a tiny inline pre-hydration click capture in
entry.tsthat queues early Play / Quick-Start clicks and replays them the millisecond UI listeners mount.
Result: 273 → 159 requests (-41.8%), 10.35 MB → 6.86 MB (-33.8%), and t_atlas_visible_ms cut in half (613.7 ms → 277.4 ms).
Bottleneck 2: Full-Resolution Bloom & 33 Offscreen FBO Passes on the Landing Preview
Our EffectComposer post-processing stack (UnrealBloomPass + miniature finish pass + output pass) was running at full 2× DPR (1920×1732) even inside the landing page's Island Atlas preview, burning 33.5 offscreen FBO switches per frame and 313 draw calls.
Meanwhile, in combat, the directional shadow map was re-rendering the entire static island + 50+ stationary towers every single frame even when only enemies moved or nothing changed on the grid.
The Fix
-
Lightweight Atlas Preview Pipeline: Disabled heavy multi-pass FBO bloom on the Atlas preview canvas and capped preview pixel ratio (
1280×1155instead of1920×1732), slashing FBO passes from33.5 → 0.5(-98.5%) and jumping preview framerate from52.9 → 77.3 FPS. -
Half-Resolution
UnrealBloomPass+ 2-Stage Adaptive DPR Governor: In battle, bloom runs at half resolution (visually indistinguishable on soft emissive halos, 4× fewer fragment invocations), and a hysteresis-guarded 2-stage adaptive governor smoothly steps down bloom/DPR only when sustained frame time rises. -
Explicit Pass Disposal: Properly disposed custom
ShaderPass/OutputPassresources on teardown to prevent WebGL context leaks across island switches. -
Static Shadow Caching: Enabled dirty-flagged shadow updates (
shadowMap.autoUpdate = false) so shadow maps only re-bake when towers are placed/upgraded/sold or the sun angle crosses a threshold.
Bottleneck 3: The Hidden 17–21ms Main-Thread Spike Was… Regex i18n Scans!
This was the biggest surprise of the entire profile.
Our English combat session showed avg 4.16 ms, p95 17.2 ms, max 21.2 ms inside the requestAnimationFrame callback — with 11+ spikes over 8 ms during a single wave — while Traditional Chinese only peaked at 8.2 ms.
Why would English be 2.5× slower on the CPU than Chinese inside a 3D WebGL game?!
Tracing rafCallbackCpuMs pointed straight to localizeDynamicText() in src/i18n.ts / src/battleI18n.ts: every time HUD counters or battle logs refreshed in a non-source locale (en, ja, ko), it iterated through dozens of dynamic regex replacement rules on the same repeating strings every tick!
The Fix
- Added a bounded O(1) lookup cache (
Mapwith deterministic eviction) keyed by(locale, rawText)inlocalizeDynamicText(), plus fast-path short-circuiting for pure numeric HUD badges. - Also diff-guarded DOM updates in
battleDom.tsso unchanged text nodes aren't rewritten every frame.
Result: EN combat rAF CPU plunged from avg 4.16 ms / max 21.2 ms down to avg 2.39 ms / max 7.7 ms — zero spikes above 8 ms.
Bottleneck 4: Procedural PBR Texture Hashing & A* Hover Re-Computation
Two smaller micro-stutters showed up whenever you moved the mouse quickly while placing towers or switched islands:
-
String-Concatenation Cache Keys in
pbrSurfaceKit3d.ts: Procedural surface synthesis was allocating temporary strings inside hot pixel loops. Replacing string keys with a pure 32-bit integer bit-mix hash (Math.imulavalanche) eliminated GC churn during island material builds. -
Redundant A* Maze Path BFS on Pointer Move (
simulation.ts): Moving the cursor by 2 pixels inside the same grid cell re-ran full multi-spawn A* path validation (updateHoverCell). Memoizing the last(gridX, gridY, buildTool, gridMutationVersion)made pointer movement inside a cell effectively O(1).
Bottleneck 5: Guarding Performance Regressions Permanently
Performance fixes rot quickly unless automated tests lock them in. We added tests/loadingPerfGuard.test.ts (part of our 119-test release suite) to assert:
- Pre-hydration click capture stays wired in
entry.ts -
localizeDynamicTextkeeps its O(1) cache -
miniaturePostFx3d.tsdisposes all custom passes (finishPass.dispose()/outputPass.dispose()) -
updateHoverCellpreserves cell + grid-version memoization - Zero deprecated
PCFSoftShadowMapwarnings
Try the 60 FPS v8.0 Build Live (No Download)
You can test the live build directly in your browser (desktop or mobile — includes a 1-tap 2D tactical mode if you're on ultra-low-power hardware):
- Play Live (EN): https://power-td.com/en/
- itch.io HTML5 (v8.0): https://powertd-dev.itch.io/power-td-grid-defense
-
Global Daily Challenge (shareable
?bp=mazes): https://power-td.com/daily
If you're profiling a heavy Three.js / WebGL web app yourself, don't assume the GPU is always guilty — half of our worst frame spikes were hiding in regex string translation and pointer-hover pathfinding on the main thread!
Top comments (0)