diff --git token.pages.inc token.pages.inc
index b57feb3..d427720 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,16 +104,31 @@ 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) {
-  $row = array(
-    'id' => _token_clean_css_identifier($token),
+  static $replacements;
+  static $parents;
+
+  if (!isset($replacements)) {
+    $replacements = _token_css_identifier_replacements();
+  }
+
+  // Build a statically cached array of default values. This is around four
+  // times as efficient as building the base array from scratch each time this
+  // function is called.
+  static $defaults = array(
+    'id' => '',
     'class' => array(),
     'data' => array(
-      'name' => $token_info['name'],
-      'token' => '',
+      'name' => '',
+      'token' => array(),
+      'class' => array(),
       'value' => '',
-      'description' => $token_info['description'],
+      'description' => '',
     ),
   );
+  $row = $defaults;
+  $row['id'] = 'token-' . strtr(trim($token, '[]'), $replacements);
+  $row['data']['name'] = $token_info['name'];
+  $row['data']['description'] = $token_info['description'];
 
   if ($is_group) {
     // This is a token type/group.
@@ -118,15 +136,18 @@ function _token_token_tree_format_row($token, array $token_info, $is_group = FAL
   }
   else {
     // This is a token.
-    $row['data']['token'] = array(
-      'data' => $token,
-      'class' => array('token-key'),
-    );
+    $row['data']['token']['data'] = $token;
+    $row['data']['token']['class'][] = 'token-key';
     if (isset($token_info['value'])) {
       $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']] = 'token-' . strtr(trim($token_info['parent'], '[]'), $replacements);
+      }
     }
   }
 
@@ -142,7 +163,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 'token-' . strtr(trim($id, '[]'), $replacements);
+}
+
+function _token_css_identifier_replacements() {
+  return array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '', ':' => '-');
 }
 
 /**
