Last active
December 12, 2015 07:58
-
-
Save tmcw/4740370 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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