testing javascript array membership, V8 oddness

Given an array that you are going to test membership against, for example if you are creating a set:

var arr = [‘some stuff’];

Instead of

arr.indexOf(‘not here’) > -1

you can

~arr.indexOf(‘not here’)

which is a bit more compact, and possibly more readable once I get used to it.

Out of curiosity, I created a benchmark to compare the two, which is ridiculous since they are both so fast.

However, the interesting thing is V8’s performance with a second pass:  http://jsperf.com/using-for-indexof-vs-1

Firefox gives results that I would have expected.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s