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 9117495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:54:31+00:00 2026-06-17T04:54:31+00:00

I never really gave much thought to garbage collection and I don’t know whether

  • 0

I never really gave much thought to garbage collection and I don’t know whether or not it is necessary to take into account when making small javascript games/applications. Any advice is appreciated, but I will ask my specific question at the end.

A lot of the time I write code of this form:

var foos=new Array();
generateFoos();
function generateFoos()
{
    foos=[];
    for (fooIndex=0;fooIndex<numberOfFoos;fooIndex++)
    {
        foos[fooIndex]=new foo(Math.random(),Math.random());
    }
} 
function foo(bar,bas)
{
   this.bar=bar;
   this.bas=bas;
}

So my question is, when I say foos=[] (line 5), does this delete the objects in that array from the memory or do they float around somewhere, making the program larger and slower? What should I do if I want to call generateFoos() a loooot of times, like every time the user presses a key.

Thanks!

  • 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-17T04:54:32+00:00Added an answer on June 17, 2026 at 4:54 am

    For a specific answer, since the accepted one doesn’t actually answer the question directly, is that yes, foo = [] does de-reference any previous values in the array.

    As Ales says, “An object becomes eligible for garbage collection when it becomes unreachable.” Indeed, this is when the browser will clear such things from memory.

    An important point, delete DOES NOT GARBAGE COLLECT.

    You see this over and over, and even in the comments on this question. The delete keyword removes a property from an object and has nothing to do with garbage collection.

    I also wanted to offer some advice on your code itself.

    1) Use literals, not new, for basic data types

    2) Don’t call functions before you declare them. Yes, it works but its messy and harder to read later. Remember, you spend much more time reading your code than writing it. Make it easy to follow later.

    3) Remember your function scope. Any variable declared without var goes global. With var, it remains within the scope of the function that contains it. Another way variables are scoped within a function is when they are passed in as named parameters.

    4) Use var on your functions when creating them. In your code, your functions are globals.

    5) Use spacing. Density of text is not next to godliness. You might be 20-something now with great eyesight, but you’ll appreciate white space in just a very few short years.

    6) Declare counters in for loops with var. Unless you want them to be global. And you almost never will.

    Lets re-work your code now:

    var numberOfFoos = 10,
        foos = [];
    
    var generateFoos = function(){
    
        foos = [];
    
        for( var fooIndex = 0; fooIndex < numberOfFoos; fooIndex++ ){
    
            foos[ fooIndex ] = new foo( Math.random(), Math.random() );
    
        }
    
    },
    
    foo = function( bar, bas ){
    
       this.bar = bar;
       this.bas = bas;
    
    }
    
    generateFoos();
    console.log( foos );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had never really thought about whether a symbol could be a number in
I never really know what order is the best installation or if it even
I've never really done much with parsing text in PHP (or any language). I've
I have never really done much C but am starting to play around with
I have never really used regular expressions all that much and as such i
I never really know how to name VBA modules. Should I: Use a prefix,
I've never really thought about this, but it helps with some security of something
I never really thought about this until I was explaining some clojure code to
I have never really studied regex so do not really ungerstand it I have
I've never really run into this in the past, so I'm curious. Can you

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.