Building a nested tree structure from an ordered list
First, nestComments appears very long. Functions that usually do too much are also functions that become harder to test. Also, code that is is visually long is usually hard to understand. Consider splitting it off into smaller, manageable functions. 

Now one thing that makes code slow is when you nest loops. In this case, you have 3 levels, 2 for loops and findIndex which is essentially a loop. Nesting loops can cause operations to explode exponentially. What you'd want to do is keep it to a minimum, since some operations really require nested loops.

Next would be trimComments. The problem of having a switch (or any code that does comparison against a hard set of items) is that you have to play catch-up with the things you need to compare. You will constantly dive into code to add things you never wrote in the first place.

Also, your trimComments is just sending in a value and expecting a result that's pretty 1:1, reminiscent of a key-value pair. This can easily be done with regular objects instead of a switch statement. You can find the input from the keys, and get its corresponding value.

Looking for how trimComments is used, I see that you use it to determine if a comment is included. Ok, cool, you use it to skip a push. But the problem is that you've already done processing, then just suddenly skip it from being included. That's a waste of operations. Also, you can preprocess your paths so that you only include the intended maximum number of comment paths, and operate on that instead.

Here's my take on it:


Normalize your input by working on full paths.
Trim down the paths based on maximums.
Operate on the remaining paths.


And the code would be broken down into:

const commentsPerLevel = {
  '1': 5 
  '2': 5
  '3': 3
  '4': 2
  '5': 1
  '6': 1
}

function getMaxForLevel(level){
  // returns value from commentsPerLevel or a default value
  return commentsPerLevel[level] || 0;
}

function getCommentPaths(comments){
  // returns full paths from input
  return comments.map(comment =&gt; `0-${comment.path}-${comment.comment_id}`);
}

function trimPaths(paths){
  // returns paths trimmed based on maximums from commentsPerLevel
}

function buildCommentTree(paths){
  // build comment tree based on paths
}

function nestComments(comments){
  const paths        = getCommentPaths(comments);
  const trimmedPaths = trimPaths(paths);
  const commentTree  = buildCommentTree(trimmedPaths);
  return commentTree;
}


I left out implementation for some, but the idea is to break down your operations into functions so they won't be that hard to maintain.