Replace is_object/is_array with switch on gettype

When checking multiple types, this is ~10% slower in the worst case, and ~30-33% faster for the average and best case.

See #218
This commit is contained in:
Justin Hileman
2014-08-18 05:00:16 -07:00
parent 6859d82e8e
commit 3d37a88f0b
2 changed files with 34 additions and 24 deletions
+10 -3
View File
@@ -172,8 +172,9 @@ class Mustache_Context
private function findVariableInStack($id, array $stack) private function findVariableInStack($id, array $stack)
{ {
for ($i = count($stack) - 1; $i >= 0; $i--) { for ($i = count($stack) - 1; $i >= 0; $i--) {
if (is_object($stack[$i]) && !($stack[$i] instanceof Closure)) { switch (gettype($stack[$i])) {
case 'object':
if (!($stack[$i] instanceof Closure)) {
// Note that is_callable() *will not work here* // Note that is_callable() *will not work here*
// See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods // See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods
if (method_exists($stack[$i], $id)) { if (method_exists($stack[$i], $id)) {
@@ -183,9 +184,15 @@ class Mustache_Context
} elseif ($stack[$i] instanceof ArrayAccess && isset($stack[$i][$id])) { } elseif ($stack[$i] instanceof ArrayAccess && isset($stack[$i][$id])) {
return $stack[$i][$id]; return $stack[$i][$id];
} }
} elseif (is_array($stack[$i]) && array_key_exists($id, $stack[$i])) { }
break;
case 'array':
if (array_key_exists($id, $stack[$i])) {
return $stack[$i][$id]; return $stack[$i][$id];
} }
break;
}
} }
return ''; return '';
+6 -3
View File
@@ -113,9 +113,11 @@ abstract class Mustache_Template
*/ */
protected function isIterable($value) protected function isIterable($value)
{ {
if (is_object($value)) { switch (gettype($value)) {
case 'object':
return $value instanceof Traversable; return $value instanceof Traversable;
} elseif (is_array($value)) {
case 'array':
$i = 0; $i = 0;
foreach ($value as $k => $v) { foreach ($value as $k => $v) {
if ($k !== $i++) { if ($k !== $i++) {
@@ -124,7 +126,8 @@ abstract class Mustache_Template
} }
return true; return true;
} else {
default:
return false; return false;
} }
} }