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
Top comments (3)
One extra test I would add around
WebAutomaticTextReplacementEnabled: becauseregister(defaults:)only populates the registration domain, an explicit persistent value for that key still takes precedence. It would be useful to cover both a fresh defaults domain and one with the key already set to true, rather than treating registration as an unconditional override.There is also a scope boundary: applying the recommendation before the first
WKWebViewis created makes the workaround effectively process-wide for web views that consult the same default, not an editor-local setting. If the app can host other web content, that trade-off is worth calling out.Both fair points. I used
register(defaults:)on purpose so nothing gets written to the user’s preferences, but you’re right that it only fills the registration domain: ifWebAutomaticTextReplacementEnabledwas ever written to the app’s own domain as true, that value wins and the freeze comes back. A test against a fresh domain and one with the key already set is the right way to cover it. And yes, the effect is process-wide: every WKWebView in that process reads the same default, so an app that also shows other web content loses text replacement there too. Here the editor is the only web view, so I accepted that, but it’s worth saying plainly.Since the cost stays flat however short the paragraph is, that rules out your own script early, which is a good way to read that sample. One tradeoff I'd weigh is that turning replacement off in the editor also drops shortcuts people may expect to expand while they write. Did you consider only disabling it when the replacement list is large?