Skip to content

Instantly share code, notes, and snippets.

@tmcw
Last active December 12, 2015 07:58
Show Gist options
  • Select an option

  • Save tmcw/4740370 to your computer and use it in GitHub Desktop.

Select an option

Save tmcw/4740370 to your computer and use it in GitHub Desktop.
// a pattern I've found useful for performance:
function trueObj(arr) {
var o = {};
for (var i = 0, l = arr.length; i < l; i++) {
o[arr[i]] = true;
}
return o;
}
// when you have a large object of lookup values:
// for instance, ids that you want to filter in
lookup = ['a', 'b', 'c'];
// you can use indexOf, but it operates in linear
// time with respect to the lookup table. that is,
// it can be slow
lookup.indexOf('a') !== -1;
// instead, you can create an object
lookupObj = trueObj(lookup);
// and check membership in it, in usually constant time
lookupObj['a'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment