Reputation: 464
When checking element presence in inline arrays via the in
keyword, CoffeeScript ignores indexOf
and rather does that many equality checks.
However, when referring to an array via a variable, it calls indexOf
as expected.
Input
foo = 1
# -----------
foo in [1, 2, 3, 4, 5]
# -----------
bar = [1, 2, 3, 4, 5]
foo in bar
Output
var bar, foo,
indexOf = [].indexOf;
foo = 1;
// -----------
foo === 1 || foo === 2 || foo === 3 || foo === 4 || foo === 5;
// -----------
bar = [1, 2, 3, 4, 5];
indexOf.call(bar, foo) >= 0;
Demo code: Link
I find this intriguing. Any ideas why?
Upvotes: 0
Views: 47
Reputation: 464
Apparently, this was an intended optimization to spare an array allocation.
From: https://github.com/jashkenas/coffeescript/issues/5392
Upvotes: 1