Programming is exhausting, especially if you type more than you have to. I’m always looking for ways to improve performance—not only regarding CPU and memory usage, but also with my approach. And, there are two points of view—the application’s and the developer’s. Sure, you can program a function that cuts a few milliseconds off your previous method, but if it’s not as legible, it might not be worth it.
In my dilemma, I’m thinking of a different approach to code that is already lightning fast. Why? It can be long-winded at times—you have to scroll just to see the other half of the statement. The problem is that the few alternatives all have their weaknesses. Here’s the currently used code:
1
2
3
| if ($string == "Jonnie" || $string == "loves" || $string == "Subway" || $string == "cookies") {
// code
} |
This is quick and easy for a few evaluations, but the more you accrue, the more redundant and illegible the conditional becomes. If you’re fine with this approach, you can always make it prettier with tabs and linebreaks, but formatting soaks up a lot of time.
1
2
3
4
5
6
7
8
| if (
$string == "Jonnie" ||
$string == "loves" ||
$string == "Subway" ||
$string == "cookies"
) {
// code
} |
Another solution is to use switch/case statements, which cut down on the redundancy, but also cut down on flexibility. In regards to speed, this switch/case statement is three times slower than our if statement.
1
2
3
4
5
6
7
| switch ($string) {
case "Jonnie":
case "loves":
case "Subway":
case "cookies":
// code
} |
Then, there are the creative approaches. What if we used a temporary Array of the possible Strings to match and searched using the indexOf method? It is flexible, not redundant, and low on characters.
1
2
3
| if (["Jonnie", "loves", "Subway", "cookies"].indexOf ($string) != -1) {
// code
} |
The downside is the speed—creating an array, searching the array, and comparing the result of the search. It is ten times slower than the logical or solution and a little over four times slower than the switch/case statement. Another weakness to mention is the complexity of readability—it includes a lot in a little space.
To overcome the lack of readability, you can create a global method.
1
2
3
4
5
6
7
| public function isIn ($needle:*, ...$haystack:Array):Boolean {
return ["Jonnie", "loves", "Subway", "cookies"].indexOf ($string) != -1;
}
if (isIn ($string, "Jonnie", "loves", "Subway", "cookies")) {
// code
} |
Though this makes the solution 12 times prettier, it is also 12 times slower than the logical or. The process now needs to track down the global method, use an untyped variable, and call the method.
What now? Either pick one or perhaps we can suggest something that’s currently impossible but shouldn’t be. In SQL and Python, we could easily get past all this with the following:
1
2
3
| if ($string in ("Jonnie", "loves", "Subway", "cookies")) {
// code
} |
This is fast, legible, and low on characters. Why doesn’t it work in Actionscript 3? The in operator is already used in AS3, but as an alternative to the hasOwnProperty method. You can actually write this above in AS3 without any compiler or runtime error, but you’ll get false every time. I think it’s time we learn from other programming languages and adopt the usage. I know it would save me time—possibly enough to hold off carpel tunnel while I’m still young. I’m not sure how you go about suggesting things like this, but I’m going to try.