Optimize document.grep