HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Scaling jsTree performance

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scalingperformancejstree

Problem

I am using jsTree v1.0 (latest v3.0, they withdraw support from older versions) to display the hierarchy in my application. It was good until until I started showing groups/user up to 200 levels, but now I have a new client which has levels as big as up to 5000, so this code is taking too long to execute. I have two options: either I switch to new plugin (time taking) or optimize the existing code which I am using.

```
public static List NodeList(DataTable dtMainGroup, DataTable dtMainUser, bool includeUsers)
{
List nodeList = new List();
List LStrGpId = new List();
List LStrChild = new List();

DataView dvGroup = new DataView();
DataView dvUser = new DataView();

dvGroup = dtMainGroup.DefaultView;
dvUser = dtMainUser.DefaultView;

if (dtMainGroup.Rows.Count > 0)
{
for (int i = 0; i ();
if (includeUsers)
{
node = GetUser(dvUser, dvGroup, drMain, node);
}
node = GetChild(dvGroup, dvUser, drMain, node, LStrChild, includeUsers);

nodeList.Add(node);
}
}
}
return nodeList;
}

public static JsTreeNode GetUser(DataView dvUsr, DataView dvGp, DataRow dr, JsTreeNode node)
{
dvUsr.RowFilter = "GroupId = '" + Convert.ToString(dr["GroupId"]) + "'";
if (dvUsr.Count > 0)
{
DataTable dtUser = new DataTable();
dtUser = dvUsr.ToTable();
for (int k = 0; k LStrChild, bool includeUsers)
{
dvGp.RowFilter = "ParentGroupId = '" + Convert.ToString(dr["GroupId"]) + "'";

if (dvGp.Count > 0)
{
DataTable dtChild = new DataTable();
dtChild = dvGp.ToTable();

for (int j = 0; j ();
if (includeUsers)
{
cnode = GetUser(dvUsr, dvGp, drChild, cnode);
}
cnode = GetChild(dvGp, dvUsr, drChild, cnode, LStrChild, includeUsers);

node.children.Add(cnode);
}
}
return node;
}

Solution

I don't think you'll find a single line of C# or JavaScript that will make this run faster. You need lazy loading. When the page first loads, load just the first two levels. Then clicking on a level will load the next level.

If the data in the database doesn't change very often, cache the HTML snippets sent back to the browser for a bigger boost to performance. There are only so many code changes you can make before you are just plain old trying to process to much data at once.

Context

StackExchange Code Review Q#69379, answer score: 6

Revisions (0)

No revisions yet.