Created
December 3, 2012 21:13
-
-
Save alecmce/4198110 to your computer and use it in GitHub Desktop.
How do you test for membership in AS3?
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
// option 1 | |
private static const ABC:Object = {"a":null, "b":null, "c":null} | |
private function isABC(char:String):Boolean | |
{ | |
return char in ABC; | |
} | |
// option 2 | |
private static const ABC:Vector.<String> = new <String>["a","b","c"]; | |
private function isABC(char:String):Boolean | |
{ | |
return ABC.indexOf(char) != -1; | |
} | |
// option 3? - other than using an Array, assuming that the ABC list could grow so we're not going to hard-code values, what other options do we have? |
Depends if speed is an issue. Object or dictionary access is going to be faster as its a hash map (thought i'm not sure about using "in"). Pretty sure indexOf just checks everything from the start index.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the feedback! I much prefer Dave's implementation with {"a":true, "b":true, "c":true} to #1. Always fun to ruminate on these details :)