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

  • SEARCH
  • Home
  • 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 8931439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:06:58+00:00 2026-06-15T09:06:58+00:00

I would like to make an extendDeep() function that does not make any garbage

  • 0

I would like to make an extendDeep() function that does not make any garbage for GC.

The garbage collector need to be as inactive as possible.
ref.: https://www.scirra.com/blog/76/how-to-write-low-garbage-real-time-javascript

This is the extendDeep() function I want to modify:

function extendDeep(parent, child) {
    var i, toStr = Object.prototype.toString,
        astr = "[object Array]";

    child = child || {};

    for (i in parent) {
        if (parent.hasOwnProperty(i)) {
            if (typeof parent[i] === 'object') {
                child[i] = (toStr.call(parent[i]) === astr) ? [] : {};
                extendDeep(parent[i], child[i]);
            } else {
                child[i] = parent[i];
            }
        }
    }
    return child;
}

The function does not have to return anything. since the retuned object is why the garbage is made.

It is assumed that all the propertis of the parent object is available by reference (reuse of objects)

  • 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-06-15T09:06:59+00:00Added an answer on June 15, 2026 at 9:06 am

    This is actually a more interesting question than I first thought. After reading the suggested link it is clear the articles author is advocating object pooling. So something like

    function Pool(fConstructor, nMaxSize, fCleanFunction) {
        this.aObjectPool = [];
        this.nMaxSize = nMaxSize;
        this.fCleanFunction = fCleanFunction;
        this.fConstructor = fConstructor;
    }
    
    Pool.prototype.get = function() {
        return this.aObjectPool.pop() || new this.fConstructor();
    }
    
    Pool.prototype.recycle = function(oObject) {
        if (aObjectPool.length < this.nMaxSize) {
            fCleanFunction(oObject);
            this.aObjectPool.push(oObject);
        }
    }       
    
    function wipeArray(aArray) {
        aArray.length = 0;
    }
    
    function wipeObject(oObject) {
        for (var p in obj) {
            if (obj.hasOwnProperty(p)) {
                delete obj[p];
            }
        }
    };
    
    var oArrayPool = new Pool(Array, 50, wipeArray);
    
    var oObjectPool = new Pool(Object, 50, wipeObject);
    

    could be used to implement the pool. Then you’d replace the []’s and {}’s in the extend deep function with pool.get()’s.

    Of course, for this to work you’d also need to ensure you were recycling old objects and arrays rather than just leaving them for garbage collection.

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

Sidebar

Related Questions

Would like to make anapplication in Java that will not automatically parse parameters used
I would like to make a function that takes in a list of integers
I would like to make a very simple ant script that does 1 thing,
I would like make a script using PHP (probably need JS) to send POST
I would like to make a package that would copy data from a table
I would like make an java app that communicates with a serial device(handheld scanner)
I would like to make a nightly cron job that fetches my stackoverflow page
I would like to make a child class that has a method of the
I would like to make a VERY simple photo gallery that displays 1 image
I would like to make a synchronized method, such that all objects from that

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.