Last active
August 29, 2015 14:14
-
-
Save david-binda/e0dfa3ee52253ec4b3da to your computer and use it in GitHub Desktop.
wp_is_mobile performance test
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
<?php | |
function wp_is_mobile() { | |
/*static $is_mobile; | |
if ( isset($is_mobile) ) | |
return $is_mobile;/* removed for performance testing purposes */ | |
if ( empty($_SERVER['HTTP_USER_AGENT']) ) { | |
$is_mobile = false; | |
} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.) | |
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false | |
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false | |
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false | |
|| strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false | |
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false | |
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) { | |
$is_mobile = true; | |
} else { | |
$is_mobile = false; | |
} | |
return $is_mobile; | |
} | |
function wp_is_mobile_regex() { | |
if ( empty($_SERVER['HTTP_USER_AGENT']) ) { | |
$is_mobile = false; | |
} elseif( preg_match( "/(Mobile|Android|Silk|Kindle|BlackBerry|Opera\sMini|Opera\sMobi)/", $_SERVER['HTTP_USER_AGENT'] ) ) { | |
$is_mobile = true; | |
}else { | |
$is_mobile = false; | |
} | |
return $is_mobile; | |
} | |
function microtime_float() { | |
list($usec, $sec) = explode(" ", microtime()); | |
return ((float)$usec + (float)$sec); | |
} | |
//number of performed loops | |
$loops = 100000; | |
$user_agents = array( | |
'Chrome' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36', | |
'iPhone' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B329 Safari/8536.25', | |
'Opera Mobile' => 'Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02' | |
); | |
foreach( $user_agents as $browser => $user_agent ) { | |
$_SERVER['HTTP_USER_AGENT'] = $user_agent; | |
//WP default function | |
$time_start = microtime_float(); | |
for( $i = 0; $i < $loops; $i++ ) { | |
$is_mobile = wp_is_mobile(); | |
} | |
$time_end = microtime_float(); | |
$result = ( true === $is_mobile ) ? 'true' : 'false'; | |
$time = $time_end - $time_start; | |
echo "Performed {$loops} wp_is_mobile checks with result {$result} on {$browser}'s UA in {$time} seconds<br/>"; | |
//regex alternative | |
$time_start = microtime_float(); | |
for( $i = 0; $i < $loops; $i++ ) { | |
$is_mobile = wp_is_mobile_regex(); | |
} | |
$time_end = microtime_float(); | |
$result = ( true === $is_mobile ) ? 'true' : 'false'; | |
$time = $time_end - $time_start; | |
echo "Performed {$loops} wp_is_mobile_regex checks with result {$result} on {$browser}'s User Agent String in {$time} seconds<br/><br/>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: