Checking for a value

Reasoning behind the bench

I’ve been working a bit on my CSSParser (which I will rename to CSSReader og CSSHandler when I get around to it), and one thing I found out was, when reading shorthand values (like: border: 1px solid #00ffff;) you can’t assume they always come in the same order.
If we take a border property as an example:

.something {
  border-right: 1px dashed #00ff00;
  border-left: dashed 1px rgb(255,0,0);
  border-top blue solid 1px;

This shows that the border shorthand value can have a different order of subvalues, and the actual value data can change alot (notice the 3 different ways of defining a color, the rgb() still cause me a bit of trouble). So to set an array (mapped with [‘width’], [‘style’] and [‘color’]) I can’t just split the string and set it, I need to split the string and then try to guess every value. To guess the values I’ll be running through the following process:

I’ve tried to put the checks in the order I would assume most common, so I can continue the loop quickly after each check and skip the following ones.

The test

Link to test

Now the thing is I need to check if one value exists in a predefined list of values (this case it’s particularly the styles, but colors and width as well has some constant names they can use to define the numeric value). I figured there’s 3 ways of doing this. Actually I started with 5 ways, but as I learned previously I have to keep these tests down in size for it to work properly (the 2 ways I cut will be grayed in the list below):

  1. Having an array with all the predefined values, and checking it with in_array.
  2. Having all the predefined values in a string separated by spaces, then using strpos to look for our value.
  3. Flipping (array_flip) our array from earlier, and instead of in_array we use isset($array[‘our_value’]).
  4. Again using strpos to check, but instead of a string separated by spaces I tried a constant.
  5. Tried a preg_match on a string of values.

I’ve run the test for the same dataset, but testing for values on different positions in the set.

  • first value, is testing for the value which is placed as the first value in the dataset.
  • mid value, is a value there’s somewhere the middle of the set.
  • last value, is last value of the set.
  • no hit, here I test for a value that doesn’t exist in the set.

The obvious winner is the isset method which is almost 5 times faster than checking for the value using the in_array (which was what I would have chosen to start with). To avoid using the array_flip you can always just write the array up with keys and then equal some value, like:

$arr = array("hidden" => 0, "dotted" => 0, "dashed" => 0);

It’s just a hassle to write larger sets of data that way, so using the array_flip function really helps out here, which is also the method I’ve chosen for my CSS project.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>