highlight-words-core icon indicating copy to clipboard operation
highlight-words-core copied to clipboard

cache regex in map

Open kellyrmilligan opened this issue 6 years ago • 3 comments

caches the regex if the same word is repeated in the search words array and uses the search word as the key.

kellyrmilligan avatar Sep 19 '18 21:09 kellyrmilligan

I'm a bit wary of an unbounded caches. RegExp objects are pretty small, but even so– this would cause us to slowly leak memory over time. Probably not a big concern, but I also think the construction of a new RegExp object is also pretty fast and so not a big concern.

We could get a bigger "win" by caching search results (aka the return value of defaultFindChunks) but we'd also leak faster that way unless we used some kind of LRU.

I'm curious what prompted this change? 😄

bvaughn avatar Sep 20 '18 17:09 bvaughn

nothing much prompted it, using react-highlight-words on some search stuff, and was just looking through core and thought it would be a nice little optimization. would it clean it up memory wise if we cleared out the object before returning the chunks? also fine to close.

kellyrmilligan avatar Sep 20 '18 18:09 kellyrmilligan

Gah. I read this too quickly this morning and misread it. Your cache is already going out of scope when defaultFindChunks finishes running so it won't leak. 😦 The partial diff view had me thinking it was defined outside of the method.

That being said, in the location it's defined in– I don't think it's likely to hit cache matches. (It wouldn't unless the same word is used multiple times within a single search string, e.g. "foo bar foo".)

bvaughn avatar Sep 20 '18 21:09 bvaughn