diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 5ce3ba9..f3d29d6 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -414,40 +414,6 @@ function comment_node_view_alter(array &$build, EntityInterface $node, EntityVie
 }
 
 /**
- * Calculates the indentation level of each comment in a comment thread.
- *
- * This function loops over an array representing a comment thread. For each
- * comment, the function calculates the indentation level and saves it in the
- * 'divs' property of the comment object.
- *
- * @param array $comments
- *   An array of comment objects, keyed by comment ID.
- */
-function comment_prepare_thread(&$comments) {
-  // A counter that helps track how indented we are.
-  $divs = 0;
-
-  foreach ($comments as $key => &$comment) {
-    // The $divs element instructs #prefix whether to add an indent div or
-    // close existing divs (a negative value).
-    $comment->depth = count(explode('.', $comment->getThread())) - 1;
-    if ($comment->depth > $divs) {
-      $comment->divs = 1;
-      $divs++;
-    }
-    else {
-      $comment->divs = $comment->depth - $divs;
-      while ($comment->depth < $divs) {
-        $divs--;
-      }
-    }
-  }
-
-  // The final comment must close up some hanging divs
-  $comments[$key]->divs_final = $divs;
-}
-
-/**
  * Generates an array for rendering a comment.
  *
  * @param \Drupal\comment\CommentInterface $comment
@@ -661,7 +627,6 @@ function comment_node_update_index(EntityInterface $node, $langcode) {
         $comments = \Drupal::entityManager()->getStorage('comment')
           ->loadThread($node, $field_name, $mode, $comments_per_page);
         if ($comments) {
-          comment_prepare_thread($comments);
           $build[] = \Drupal::entityManager()->getViewBuilder('comment')->viewMultiple($comments);
         }
       }
diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php
index 6a4760b..6fd7448 100644
--- a/core/modules/comment/src/CommentViewBuilder.php
+++ b/core/modules/comment/src/CommentViewBuilder.php
@@ -16,6 +16,7 @@
 use Drupal\Core\Entity\EntityViewBuilder;
 use Drupal\entity\Entity\EntityViewDisplay;
 use Drupal\Core\Language\LanguageManagerInterface;
+use Drupal\Core\Render\Element;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
@@ -88,6 +89,8 @@ public function buildComponents(array &$build, array $entities, array $displays,
     }
     $this->entityManager->getStorage('user')->loadMultiple(array_unique($uids));
 
+    self::buildDepth($build, $entities);
+
     parent::buildComponents($build, $entities, $displays, $view_mode, $langcode);
 
     // Load all the entities that have comments attached.
@@ -281,6 +284,48 @@ protected static function buildLinks(CommentInterface $entity, EntityInterface $
   }
 
   /**
+   * Calculates the indentation level of each comment in a comment thread.
+   */
+  protected static function buildDepth(array &$build, array $comments) {
+    // 'divs' properties are set to 1, 0 or a negative value, instructing
+    // $this->alterBuild() to add an indent div or close existing divs.
+
+    // Do this only once for a render array:
+    if (empty($build['#comment_divs_built'])) {
+
+      $current_depth = 0;
+      $current_key = '';
+      foreach (Element::children($build, TRUE) as $key) {
+        if (isset($comments[$key])) {
+          $comments[$key]->depth = count(explode('.', $comments[$key]->getThread())) - 1;
+          if ($comments[$key]->depth > $current_depth) {
+            // Set 1 to indent this from the previous one (its parent). Set only
+            // one extra level of indenting even if the difference in depth is
+            // higher.
+            $comments[$key]->divs = 1;
+            $current_depth++;
+          }
+          else {
+            // Set zero if this comment is on the same level as the previous one
+            // or (negative) amount of divs to close to get to this comment's
+            // indent level.
+            $comments[$key]->divs = $comments[$key]->depth - $current_depth;
+            $current_depth = $comments[$key]->depth;
+          }
+
+          $current_key = $key;
+        }
+      }
+      if ($current_key) {
+        // The final comment must close up some hanging divs.
+        $comments[$current_key]->divs_final = $current_depth;
+      }
+
+      $build['#comment_divs_built'] = TRUE;
+    }
+  }
+
+  /**
    * {@inheritdoc}
    */
   protected function alterBuild(array &$build, EntityInterface $comment, EntityViewDisplayInterface $display, $view_mode, $langcode = NULL) {
diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
index e5ad723..2756772 100644
--- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
+++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
@@ -156,7 +156,6 @@ public function viewElements(FieldItemListInterface $items) {
         $comments_per_page = $comment_settings['per_page'];
         $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id'));
         if ($comments) {
-          comment_prepare_thread($comments);
           $build = $this->viewBuilder->viewMultiple($comments);
           $build['pager']['#theme'] = 'pager';
           if ($this->getSetting('pager_id')) {
