In search of the best solution—speed and size

 

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.

5 replies

  1. It really is refreshing to see a developer working hard on optimisation rather than simply cramming in more features than really necessary to applications with no concern for real-world usability. If only all developers thought the way you do, the world would be a happier, snappier place, and no doubt, there would be more cookies for all.

  2. Subway cookies are scrumptious.

    Also, I enjoy storing certain types of data in an SQL database for the exact purpose you stated. `IN` is so valuable but so rarely used (or maybe known) by programmers.

  3. I think about this very frequently. I often code AS3 more than 50 hours a week and it is physically tiring to me after 12 hours or so. I start to notice pain points like the left pinky finger from hitting and holding the shift key so often. This leads me to capitalize less and loathe C++/C#-style CapitalizedFunctionNames and CapitalizedVariableNames. I don’t take it to extremes and lower-case everything, of course, but exercises like you have written about here are common for me. As for your example, I have two suggestions:

    1) Drop the sigils and save a character ;-)
    2) if ($string in {Jonnie:1, loves:1, Subway:1, Cookies:1})

    Obviously you could use whatever you want instead of the value 1 for that technique. I recommend something easy for you to hit. It will be slow like your Array example, but it’s exactly the same number of characters as your ideal example from SQL and Python.

    I look forward to anything more you have to write about reducing the typing load. In the meantime, here is a search on my site for anything I’ve written about “tying”:

    http://jacksondunstan.com/?s=typing

    Thanks for writing this!

  4. @Jackson — I used to have that same left pinky pain when I used a PC keyboard, but now that I have the latest Apple slim keyboard, there’s a lot less stress. You should try it out.

    Also, I use a Wacom pen (http://bit.ly/u3TqK) and keep it tucked with my left thumb when typing. I can still type with that thumb, so it works out well—flip up the pen when I need to use the mouse. The trick is mapping the pen as a mouse, so you “flick” the cursor around the screen rather than mirroring it.

    I’m hesitant to lose the sigils, since they make it so easy to identify temporary vars, but I see your point :) Also, I tried out your solution and it actually is faster than the array with indexOf.

  5. Thanks for the tips. I am, unfortunately, addicted to split keyboards. I will totally try out the Wacom pen idea though. There are plenty of artists around where I work to talk out of their pens. :-D

Reply