This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(filterFilter): allow array like objects to be filtered #11787
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -425,6 +425,23 @@ describe('Filter: filter', function() { | |
toThrowMinErr('filter', 'notarray', 'Expected array but received: {"toString":null,"valueOf":null}'); | ||
}); | ||
|
||
it('should not throw an error if used with an array like object', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is obviously not sufficient. We need to test with "less array-like" objects. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'll change the test to use arguments |
||
function getArguments() { | ||
return arguments; | ||
} | ||
var argsObj = getArguments({name: 'Misko'}, {name: 'Igor'}, {name: 'Brad'}); | ||
|
||
var nodeList = jqLite("<p><span>Misko</span><span>Igor</span><span>Brad</span></p>")[0].childNodes; | ||
function nodeFilterPredicate(node) { | ||
return node.innerHTML.indexOf("I") !== -1; | ||
} | ||
|
||
expect(filter(argsObj, 'i').length).toBe(2); | ||
expect(filter('abc','b').length).toBe(1); | ||
expect(filter(nodeList, nodeFilterPredicate).length).toBe(1); | ||
|
||
}); | ||
|
||
|
||
it('should return undefined when the array is undefined', function() { | ||
expect(filter(undefined, {})).toBeUndefined(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not sufficient. We are using
array.filter
, whichisArrayLike
does not guarrantee to exist.Maybe we could change
array.filter(...)
with something likeisArray(array) ? array.filter(...) : Array.prototype.filter.call(array, ...)
.It might even be better to always use the latter form regardless of the type of
array
.This needs a little more investigation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think is better to use the latter form always.
I made this microbench: http://jsperf.com/with-or-without-isarray
Even assuming that using arrays is the most common case, I don't think that the performance gain is worth having the check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the jsperf (http://jsperf.com/with-or-without-isarray/2) to reuse
Array.prototype.filter
(although that should hardly make any noticable difference).Basically, 99% of the time, I believe we will be dealing with arrays, so that is the usecase we should be optimizing for. So, we are mainly concerned about
array with check
vsarray without check
(usingArray.prototype.filter
).Indeed, on desktop Chrome and Firefox, both seem to be almost equally fast.
On mobile Chrome, the latter was about 10% slower.
More importantly, on desktop IE11, the latter was consistently >15% slower.
So, I would go with the former.
We should:
1.) Store
Array.prototype.filter
(if nothing else for readability)2.) Allow
array-like
objects.3.) Check
array
and decide what function to call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of my poor english I normally don't write down my arguments too well.
I'll try to explain my point a little more (however, and of course, I don't have any problem to change the PR to make the check).
In the jsperf, we see very little difference given between
array with check
andarray without check
that:In a more realistic scenario where we may be concerned by performance, I think we'll have this differences:
predicateFn
will be one created bycreatePredicateFn
, that will be way slower than the one optimized in the jsbinBy this reasons, IMHO, I think that the improvement of the check is so small when performance is an issue, that is not worth it.
But again, if you think it is worth it, I'll change the PR :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think you are right. It's fine as it is.