Skip to content
Advertisement

Faster ‘ find & reorder’ logic in javascript

I am reordering the nodes in a certain level for a tree structure using the following recursive function. Reorder using the sort is not taking time, however recursively finding the parent node is taking time. how can I make this faster?

JavaScript

tree data

JavaScript

reorderedObject

JavaScript

If i have 4 level depth and 50 items in each level, it takes 10 sec to process.

Advertisement

Answer

Your code should be fast. I’ve implemented a recursive descent parser for a family tree app similar to what you’re doing and have never exceeded a few tens of milliseconds drawing an entire family tree.

The main problem with your code is this:

JavaScript

That’s a whole lot of malloc() the javascript engine need to do in the background! Avoid copying memory in any language for speed.

If you need the original tree then clone just once before you enter the recursive function.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement