A text editor in one of my Mac apps froze while people typed. Not a crash, not a spinner: each key landed late, and on an older Mac the window simply stopped answering for a moment. The editor is a contenteditable page inside a WKWebView, which is a very ordinary setup, so I expected a slow script of my own. It was not mine at all.
Measure before guessing
I built a small harness: a real WKWebView, keys sent with insertText and real NSEvents, and sample running against both processes while it typed. Two things showed up right away.
First, on every typed character WebKit's web process asks the app's main thread to check the paragraph's text. That is a synchronous call, so the web view waits for the answer before the next key.
Second, almost all of that time was not spelling. It was NSSpellChecker building its dictionary of the user's text replacements, the shortcuts you set in System Settings > Keyboard > Text Replacements. On a Mac with a few thousand of them (emoji shortcut packs add a lot), that lookup cost about 40 ms per keystroke on an M4. Spelling, grammar, smart quotes and correction together cost about 5 ms.
40 ms per key is past the point where typing feels wrong, and it stays flat however short the paragraph is, so even a one-word note was slow. Older work Macs multiply it into a freeze.
The fix is one default
WebKit reads a user default for text replacement once, when the first web view is made. Registering it as off, before any web view exists, stops the per-key replacement scan:
UserDefaults.standard.register(defaults: [ "WebAutomaticTextReplacementEnabled": false ]) register(defaults:) writes nothing to disk; it only sets the fallback for this run. In the same harness, main-thread spell time fell from 319 ms to 9 ms per three seconds of typing, and the 95th percentile key lateness from 22 ms to 2 ms. Spelling underlines still work.
Two traps in the harness
- A harness window in the background gets App Nap timer throttling. A constant 233 ms per tick looked like a slowdown in my code until I held an activity with
beginActivity. - Turning off
spellcheckwith a MutationObserver after the editor exists never took effect. The default has to be in place before WebKit starts.
What I took from it
If a WKWebView editor feels slow only on some Macs, ask what those Macs have that yours does not. Here it was a long list of text replacements, something no test machine of mine had. The sample of the main thread named it in a minute; guessing would have taken a week.
I make small native Mac apps at AppsByMo. If stories like this are useful to you, I send a short email when an app ships: appsbymo.app