Skip to content

Instantly share code, notes, and snippets.

@david-binda
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david-binda/e0dfa3ee52253ec4b3da to your computer and use it in GitHub Desktop.
Save david-binda/e0dfa3ee52253ec4b3da to your computer and use it in GitHub Desktop.
wp_is_mobile performance test
<?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/>";
}
@david-binda
Copy link
Author

Results:

Performed 100000 wp_is_mobile checks with result false on Chrome's UA in 0.25132203102112 seconds
Performed 100000 wp_is_mobile_regex checks with result false on Chrome's User Agent String in 1.9904379844666 seconds

Performed 100000 wp_is_mobile checks with result true on iPhone's UA in 0.064327955245972 seconds
Performed 100000 wp_is_mobile_regex checks with result true on iPhone's User Agent String in 1.7663469314575 seconds

Performed 100000 wp_is_mobile checks with result true on Opera Mobile's UA in 0.11455106735229 seconds
Performed 100000 wp_is_mobile_regex checks with result true on Opera Mobile's User Agent String in 0.37428307533264 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment