.bind() For Dummies
Dave Lunny

Your use of `bind` certainly solved the “issue” of the context var, for developers who find that a concern.

But `bind` can also be used to (partially) apply arguments, which I think at least deserves a mention, if not some example uses.

I created a performance test for your two solutions: http://jsperf.com/bind-efficiency. Context won over bind on my browser, possibly due to the creation and GC of an additional function.

But I think there is a larger performance concern in your code. Checking every item in the list when you add a new item will result in O(N*N) performance when adding a multiple number of items (flavors).

An easy way to solve that is to use a hashtable, which is readily available in Javascript as an empty object:

var store = {};

// We can check if the flavor already exists in O(1) time
if (!flavors[newFlavor]) { flavors[newFlavor] = true;

Adding multiple flavors stored in that manner will be O(N).

The list of flavors can be obtained from Object.keys(store) or with a for..in loop.

Show your support

Clapping shows how much you appreciated Joey Twiddle’s story.