I recently experienced really slow full text searches in VS Code. These are the steps that fixed that behavior for me. Since I switched Macbooks recently, I suspected that I had missed some OS defaults.
First, I verified that this is not related to any of my numerous Extensions in use by starting VS Code without Extensions: code --disable-extensions.
In my case, searches were still slow, so this confirmed it’s not an issue with an Extension.
Exclude file-heavy, irrelevant folders
Next, I opened my VS Code Settings, switched to the file mode (to edit the JSON version) and added the following:
...
"search.exclude": {
"**/node_modules": true,
"**/.git": true,
"**/dist": true,
"**/build": true,
"**/.next": true
},
"files.watcherExclude": {
"**/node_modules/**": true,
"**/.git/objects/**": true,
"**/dist/**": true,
"**/build/**": true
},
"search.followSymlinks": false
...
This is supposed to reduce polluting my results with irrelevant files – while at the same time speeding up the search. As a PHP developer, I explicitly want to find files in vendor – which is why I use the Search vendor Extension. But you might want to exclude other folders here, depending on your projects.
Reset macOS file indexing
Furthermore, since I always have dozens of large projects on my disk, I decided to explicitly disable Spotlight indexing for my web projects folder. This can reduce the need for macOS to do background scanning of folders. VS Code uses ripgrep for its search function, so it does not rely on, require or use the Spotlight index.
So under System Settings -> Spotlight I deactivated all sorts of file types which were enabled by design. Then hit Search Privacy… and explicitly added my web projects folder to the exclude list.
Finally, after making these changes to Spotlight, I ran sudo mdutil -E / to force rebuilding the Spotlight index.
Review macOS File Watcher Limts
In my case ulimit -n was set (by design) to really low value of 256 – this was most likely causing the slow searches. For best performance you want something much bigger, eg. 65536.
Also, my system-wide defaults were too low. launchctl limit maxfiles.
For a permanent fix, I created /Library/LaunchDaemons/limit.maxfiles.plist and pasted the following:
/Library/LaunchDaemons/limit.maxs.plist<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>limit.maxfiles</string> <key>ProgramArguments</key> <array> <string>launchctl</string> <string>limit</string> <string>maxfiles</string> <string>524288</string> <string>524288</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>
Then to register the new LaunchDaemon:
# set permissions and load
sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
sudo chmod 644 /Library/LaunchDaemons/limit.maxfiles.plist
sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
Clear VS Code Caches
Finally, I emptied VS Code caches.
# empty caches
rm -rf ~/Library/Application\ Support/Code/Cache
rm -rf ~/Library/Application\ Support/Code/CachedData
Finally, I rebooted and yes, I am back to a speedy file search in VS Code.