diff --git token.pages.inc token.pages.inc
index b57feb3..214c5a3 100644
--- token.pages.inc
+++ token.pages.inc
@@ -15,12 +15,14 @@ function theme_tree_table($variables) {
   foreach ($variables['rows'] as &$row) {
     $row += array('class' => array());
     if (!empty($row['parent'])) {
-      $row['class'][] = 'child-of-' . drupal_clean_css_identifier($row['parent']);
+      // $row['parent'] is already a valid class name here, so no need to call
+      // drupal_clean_css_identifier()
+      $row['class'][] = 'child-of-' . $row['parent'];
       unset($row['parent']);
     }
   }
 
-  if (count($variables['rows'])) {
+  if (!empty($variables['rows'])) {
     drupal_add_library('token', 'treeTable');
   }
 
@@ -42,6 +44,7 @@ function theme_token_tree($variables) {
   elseif ($variables['global_types']) {
     $token_types = array_merge($token_types, token_get_global_token_types());
   }
+  $multiple_token_types = count($token_types) > 1;
 
   $header = array(
     t('Name'),
@@ -55,7 +58,7 @@ function theme_token_tree($variables) {
       continue;
     }
 
-    if (count($token_types) > 1) {
+    if ($multiple_token_types) {
       $row = _token_token_tree_format_row($type, $type_info, TRUE);
       unset($row['data']['value']);
       $rows[] = $row;
@@ -71,7 +74,7 @@ function theme_token_tree($variables) {
       if (!empty($token_info['restricted']) && empty($variables['show_restricted'])) {
         continue;
       }
-      if (count($token_types) > 1 && !isset($token_info['parent'])) {
+      if ($multiple_token_types && !isset($token_info['parent'])) {
         $token_info['parent'] = $type;
       }
       $row = _token_token_tree_format_row($token, $token_info);
@@ -101,8 +104,16 @@ function theme_token_tree($variables) {
  * Build a row in the token tree.
  */
 function _token_token_tree_format_row($token, array $token_info, $is_group = FALSE) {
+  static $replacements;
+  static $parents;
+
+  if (!isset($replacements)) {
+    $replacements = _token_css_identifier_replacements();
+  }
   $row = array(
-    'id' => _token_clean_css_identifier($token),
+    // Use an inline version of _token_clean_css_identifier() since this can be
+    // called thousands of times during a request.
+    'id' => strtr('token-' . trim($token, '[]'), $replacements),
     'class' => array(),
     'data' => array(
       'name' => $token_info['name'],
@@ -126,7 +137,12 @@ function _token_token_tree_format_row($token, array $token_info, $is_group = FAL
       $row['data']['value'] = $token_info['value'];
     }
     if (!empty($token_info['parent'])) {
-      $row['parent'] = _token_clean_css_identifier($token_info['parent']);
+      if (isset($parents[$token_info['parent']])) {
+        $row['parent'] = $parents[$token_info['parent']];
+      }
+      else {
+        $row['parent'] = $parents[$token_info['parent']] = strtr('token-' . trim($token_info['parent'], '[]'), $replacements);
+      }
     }
   }
 
@@ -142,7 +158,15 @@ function _token_token_tree_format_row($token, array $token_info, $is_group = FAL
  * @see drupal_clean_css_identifier().
  */
 function _token_clean_css_identifier($id) {
-  return drupal_clean_css_identifier('token-' . trim($id, '[]'), array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '', ':' => '-'));
+  static $replacements;
+  if (!isset($replacements)) {
+    $replacements = _token_css_identifier_replacements();
+  }
+  return strtr('token-' . trim($id, '[]'), $replacements);
+}
+
+function _token_css_identifier_replacements() {
+  return array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '', ':' => '-');
 }
 
 /**
