I have just finished up making an object Depth Management system that I will be using for drawing and explicitly drawing. Because we all know that the order in which things are drawn heavily effects the way the game will look.My system has a very simple and easy to follow code, and the only problems I have found are that you must have access to the objects you added to the manager. You cannot explicitly delete or create depths, only add objects with depths. Here's the entire code:function DepthManager() {    this.uniqueKey = DM + DepthManager.prototype.version;    this.ukD = this.uniqueKey + _d;    this.ukX = this.uniqueKey + _x;    this.ukA = this.uniqueKey + _a;    DepthManager.prototype.version += 1;    this.depthContainer = {};    this.depthsList = [];}DepthManager.prototype.version = 0;DepthManager.prototype.addArray = function(arr, depth){  var dArray, obj;  while(!this.depthContainer.hasOwnProperty(depth)){    this.add(arr[0],depth);    arr.shift();  }  for( var i = 0; i < arr.length; i ++ ){    obj = arr[i];    // Can only create it once    if( obj[this.ukA] )continue;    obj[this.ukA] = true;    // Lets push it onto an already existing depth    dArray = this.depthContainer[depth];    dArray.push(obj);    obj[this.ukD] = depth;    obj[this.ukX] = dArray.length;  }};DepthManager.prototype.add = function (obj, depth) {    var dArray;    // Can only create it once    if( obj[this.ukA] )return console.warn(Can't be placed twice);    obj[this.ukA] = true;    // Lets push it onto an already existing depth    //console.log(this.depthContainer);    if (this.depthContainer.hasOwnProperty(depth)) {        dArray = this.depthContainer[depth];        dArray.push(obj);        obj[this.ukD] = depth;        obj[this.ukX] = dArray.length;        } else {// make a new depth        dArray = [obj];        this.depthContainer[depth] = dArray;        obj[this.ukD] = depth;        obj[this.ukX] = dArray.length;        // Add it into depths list, sort it        this.depthsList.push(depth);        this.depthsList.sort();    }};DepthManager.prototype.remove = function (obj) {    var pos = obj[this.ukD];    if(!obj[this.ukA])return console.warn('not in the depth');    obj[this.ukA] = false;    var dArray = this.depthContainer[pos];    if (dArray.length === 1) {        var delIndex = this.depthsList.indexOf(pos);        this.depthsList.splice(delIndex,1);        return delete this.depthContainer[pos]    }    var posX = obj[this.ukX]-1;    for(var i = posX; i < dArray.length; i ++ ){        dArray[i][this.ukX]--;    }    dArray.splice(posX,1);};DepthManager.prototype.jump = function(obj, depth){    this.remove(obj);    this.add(obj,depth);};DepthManager.prototype.flat = function(){    var DC = this.depthContainer;    return this.depthsList.reduce(function(flat, prop){       return flat.concat( DC[prop]);    }, []);  };dm = new DepthManager();var fake = {name:'fake'}, maked = {name:'maked'};// Adding a single objectdm.add(fake, 5);// Removing that object dm.remove(fake);// Adding an array of objects (optimized for performance)dm.addArray([fake,maked],5);// Removing object againdm.remove(fake);//Jumping that object, even if not in manager worksdm.jump(fake, 6);//Jumping to new depth worksdm.jump(maked, 7);// 5,000 Bench test (lots of layers is the only bottleneck)for( var i = 0; i < 5000; i ++ ){    dm.add({}, Math.floor(i/10));}console.log(done generating depth manager);var arr = [];for(var i = 0; i < 100; i ++ ){    arr.push(dm.flat());}console.log(done concatting 5000 objects managed by depth 100 times);//console.log(arr);// flattens out How could this be improved? Or would you just completely recommend against it? It actually fairs completely well and really, the only part of the system I'll be relying on to be FAST is the flat() function. This will be called every time I draw a frame. I'm thinking maybe I could simply cache each flattened depth list and only call the real flatten code if there was a change since the last flatten. But it actually performs very quickly for a list of 5,000 entries with lots a good amount of depth layers.Any help is appreciated! JSFIDDLE
Object Depth Manager
javascript