Performance tips

ExtendScript Perf. | Given obj={k1:null,k2:null,etc.} and re=/k1|k2|etc/ obj.hasOwnProperty("foo") is about 10X faster than re.test("foo")

Using redraw = false can make a big difference.

app.scriptPreferences.enableRedraw = false;

Depending on the version of indesign that you are working with however you can try and use the FAST_ENTIRE_SCRIPT undo mode as long as you set your grep outside this FAST_ENTIRE... mode.