0

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?

2

1 Answer 1

1

Apparently, this was an intended optimization to spare an array allocation.

From: https://github.com/jashkenas/coffeescript/issues/5392

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.