Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search improvements #572

Merged
merged 6 commits into from
Jan 29, 2018
Merged

Search improvements #572

merged 6 commits into from
Jan 29, 2018

Conversation

Akryum
Copy link
Member

@Akryum Akryum commented Jan 26, 2018

@Akryum Akryum self-assigned this Jan 26, 2018
@Akryum Akryum added this to the v4.1.2 milestone Jan 26, 2018
src/util.js Outdated
if (Array.isArray(mixedValue) && searchInArray(mixedValue, stringValue.toLowerCase())) {
return true
function interalSearchCheck (searchTerm, key, value, seen, depth) {
if (depth > 10) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's okay for a hotfix, but wouldn't it be better to use something like WeakMap to make sure we're not checking an object we have checked before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's what the seen Map is for.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh lol sorry, I thought we couldn't use objects as keys in js maps, not sure why.
Why do we wait for depth to be 10, wouldn't be safe to stop if we check the same object twice while inspecting an object?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For performance reasons, I though it would be nice to have some kind of limit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we couldn't use objects as keys in js maps

It's the other way around, we can use objects in Map but not in WeakMap.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used WeakMap with objects before 😆

About the depth, I don't get what you mean with performance, if we drop the search because we found an object we already found while looking recursively into anothe object, wouldn't that be the sortest way to stop the search?

// given
{ a: { b: obj }, c: obj}

once we try to lookup obj inside of b, we would never look it up inside of c

// given
var a = {}
var b = { b: { a }}
a.b = b

we would only dive till a.b.b (or a.b.b.a depending on the implem)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I don't why I had errors with WeakMap.
If you don't have any match, we could spend a lot of time on very large objects, I thing it's a good idea to have some kind of hard limit on recursive depth (also, the algorithm can search deeply into arrays now).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay so it was to limit deep nested objects no matter repetition.
Forget about WeakMap, Map is ok 🙂

src/util.js Outdated
export function searchDeepInObject (obj, searchTerm) {
var match = false
return internalSearchObject(obj, searchTerm.toLowerCase(), new Map(), 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be safer to clear the map when leaving the function to avoid holding references to objects and creating a leak

Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested ✅

@Akryum Akryum merged commit dc75c5f into vuejs:master Jan 29, 2018
@Akryum Akryum deleted the 550-search-improvements branch January 29, 2018 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants