Array#intersect and Array#without inconsistency

0 views
Skip to first unread message

Allen

unread,
Sep 3, 2009, 1:55:07 PM9/3/09
to Prototype: Core
Hi all,

I was looking into some of the array methods and noticed this
inconsistency.
>>> [1].without("1");
[]
>>> [1].intersect(["1"]);
[]

Basically, without uses an == comparison, whereas intersect uses an
=== comparison. IMHO, I think == is more appropriate. As such, I have
forked the prototype repo and modified intersect. You can view the
diff here:

http://github.com/blatyo/prototype/commit/8b9a7b721ef787110f85c03a28c3cdb3c67dbe34

I also created a test to see how the two methods performed against
each other. See that here:

http://groups-beta.google.com/group/prototype-core/web/uniontest.tar.gz

Here are some of the results I got in milliseconds (running on
chromium):
Testing New Intersect
4517
4614
4823
4998
4856

Testing Old Intersect
7321
7337
7376
7353
7331

Let me know what you guys think.

kangax

unread,
Sep 3, 2009, 6:20:32 PM9/3/09
to Prototype: Core
On Sep 3, 1:55 pm, Allen <bla...@gmail.com> wrote:
> Hi all,
>
> I was looking into some of the array methods and noticed this
> inconsistency.>>> [1].without("1");
> []
> >>> [1].intersect(["1"]);
>
> []
>
> Basically, without uses an == comparison, whereas intersect uses an
> === comparison. IMHO, I think == is more appropriate. As such, I have
> forked the prototype repo and modified intersect. You can view the
> diff here:
>
> http://github.com/blatyo/prototype/commit/8b9a7b721ef787110f85c03a28c...
>
> I also created a test to see how the two methods performed against
> each other. See that here:
>
> http://groups-beta.google.com/group/prototype-core/web/uniontest.tar.gz
>
> Here are some of the results I got in milliseconds (running on
> chromium):
> Testing New Intersect
> 4517
> 4614
> 4823
> 4998
> 4856
>
> Testing Old Intersect
> 7321
> 7337
> 7376
> 7353
> 7331
>
> Let me know what you guys think.

There's also `uniq`, which relies on `==`:

[1, 2, 3, '1'].uniq(); [1, 2, 3]

`without` actually relies on `include`, so it is `include` that would
need to be changed. I think I'd rather see `===` used instead of `==`
where possible, although there are probably cases when `==` is more
convenient.

Btw, `NaN` inequality to each other might be confusing in context of
`uniq`. It might be worth mentioning in the docs that `[NaN, NaN].uniq
()` results in the very same `[NaN, NaN]`.

--
kangax

Allen Madsen

unread,
Sep 5, 2009, 1:32:01 PM9/5/09
to prototy...@googlegroups.com
From my perspective, I never use arrays to store objects of different types (I think that sort of thing belongs in an object of its own). So in theory I don't particularly care either way. However, == seems to be the standard way since many methods use include, which uses ==, or use == directly. I think === is particularly useful when you are trying to restrict an operation to a type in addition to the value and I don't see array or enumerable operations as needing that restriction. In addition, AFAIK, the only values that could be matched between types are numbers, strings, and booleans, which I don't see as very harmful.

Joran

unread,
Sep 7, 2009, 8:00:22 AM9/7/09
to Prototype: Core
Re: Array.uniq and Array.include and '==':

There's a bug in the existing Array.uniq where [false, 0].uniq()
returns [false]. I would prefer '===' for Array.include.

See: http://prototype.lighthouseapp.com/projects/8886/tickets/786-optimize-arrayuniq-to-return-in-on-time

Allen Madsen

unread,
Sep 7, 2009, 10:43:57 PM9/7/09
to prototy...@googlegroups.com
Tobie,

Do you have any input on this?

Tobie Langel

unread,
Sep 8, 2009, 7:33:47 AM9/8/09
to Prototype: Core
> Tobie,
> Do you have any input on this?

Yes, I'm in favor of strict equality.

Allen Madsen

unread,
Sep 8, 2009, 9:32:01 AM9/8/09
to prototy...@googlegroups.com
There seems to be more support for strict equality, so I'll write up a patch with that and modify some test cases around the change.
Reply all
Reply to author
Forward
0 new messages