Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1060965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:24:25+00:00 2026-05-16T18:24:25+00:00

I have a Django admin page for a nested category list like this: I

  • 0

I have a Django admin page for a nested category list like this:
alt text

I wrote this script to sort the list and present it hierarchically:

{% extends "admin/change_list.html" %}
{% load i18n %}
{% block footer %}

<script>
(function(){

  var rows=document.getElementById('result_list').getElementsByTagName('tr'),
      table=rows[1].parentNode||rows[1].parentElement,
      i=0, r,      // skip the first row
      data={};     // store category data

  while (r=rows[++i]) {
    var catName=r.getElementsByTagName('a')[0],
        k=catName.innerHTML,
        opts=r.getElementsByTagName('select')[0],
        j=-1, opt;
    while (opt=opts[++j]) {
      if (!opt.selected) continue;
      data[k] = {  
        title:        k,
        children:     {},
        parentName:   opt.innerHTML, 
        parentId:     opt.value, 
        catName:      catName,
        row:          r
      } 
    }
  }

  for (var sub in data) {
    if (data[sub].parentName == sub) continue;
    for (var sup in data) {
      if (sup == data[sub].parentName) {
        data[sup].children[sub]=data[sub];
        data[sub].parent = data[sup];
        break;
      }
    }
  }

  var alt = 0;
  for (var leaf in data) {
    if (data[leaf].parentName != leaf) continue;
    walk(data[leaf], leaf, function (node, nodeName) {
      var n=node, t=n.title;
      while (n=n.parent) {
        t = ' &middot; &nbsp;' + t;
      }
      node.catName.innerHTML = t;
      node.row['class']=node.row['className']='row'+alt++%2;
      table.removeChild(node.row);
      table.appendChild(node.row);
    });
  }

  function walk (leaf, leafName, cb) {
    if (cb) cb(leaf, leafName);
    leaf.ready = true;
    for (var kid in leaf.children) {
      if (leaf.children[kid].ready) continue;
      walk(leaf.children[kid], kid, cb);
    }
  }

}());
</script>

{% endblock %}

…the script runs fine and the list looks like this:
alt text

My question is: I feel like the script is prone to memory leaks in UAs with weak garbage collection because of the circular references created by the parent / child stuff. Is this something I should be worried about? Is there a better way to write the script? Should I be deleting a bunch of stuff at the end of the script, and if so, what?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:24:26+00:00Added an answer on May 16, 2026 at 6:24 pm

    I see some minor leaks, IE garbage collector has a problem cleaning live node references inside functions. This is because the DOM and JavaScript both have it’s own garbage collector and basically, they don’t want to pick a fight with eachother over a reference.

    Since you call this script once per page? The memory leak is minute and can actually be ignored, unless people open 100+ pages in one session. Cleaning up is nicer though.

    {% extends "admin/change_list.html" %}
    {% load i18n %}
    {% block footer %}
    
    <script>
    (function(){
    
      var rows=document.getElementById('result_list').getElementsByTagName('tr'),
          table={},
          i=0, r,      // skip the first row
          data={};     // store category data
    
      // table is now a JS object with a el reference to an element.
      table.el = rows[1].parentNode||rows[1].parentElement;
    
      while (r=rows[++i]) { // you skip the first row, that correct? Else use i++
        var catName=r.getElementsByTagName('a')[0],
            k=catName.innerHTML,
            opts=r.getElementsByTagName('select')[0],
            j=-1, opt;
        while (opt=opts[++j]) {
          if (!opt.selected) continue;
          data[k] = {  
            title:        k,
            children:     {},
            parentName:   opt.innerHTML, 
            parentId:     opt.value, 
            catName:      catName,
            row:          r
          } 
        }
      }
      // nullify node references
      r = catName = opt = rows =  null;
    
      for (var sub in data) {
        if (data[sub].parentName == sub) continue;
        for (var sup in data) {
          if (sup == data[sub].parentName) {
            data[sup].children[sub]=data[sub];
            data[sub].parent = data[sup];
            break;
          }
        }
      }
    
      var alt = 0;
      for (var leaf in data) {
        if (data[leaf].parentName != leaf) continue;
        walk(data[leaf], leaf, function (node, nodeName) {
          var n=node, t=n.title;
          while (n=n.parent) {
            t = ' &middot; &nbsp;' + t;
          }
          node.catName.innerHTML = t;
          node.row['class']=node.row['className']='row'+alt++%2;
          // if table wasn't a JS object, this closure would not have been cleaned up.
          // a refence to table is kept, not to a live DOM element.
          table.el.removeChild(node.row);
          table.el.appendChild(node.row);
        });
      }
    
    
      function walk (leaf, leafName, cb) {
        if (cb) cb(leaf, leafName);
        leaf.ready = true;
        for (var kid in leaf.children) {
          if (leaf.children[kid].ready) continue;
          walk(leaf.children[kid], kid, cb);
        }
      }
    
    }());
    </script>
    
    {% endblock %}
    

    Was a bit confusing to sort out, since your JS object names imply they are DOM elements =P but I think I figured it out correctly. But you might want to go over the code and nullify other DOM elements I might’ve overlooked (once you’re done with them ofcourse).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.