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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:40:51+00:00 2026-06-13T21:40:51+00:00

I am newbie to jQuery. I am bit confused whether is it fine or

  • 0

I am newbie to jQuery. I am bit confused whether is it fine or may cause memory leak ?

Here is the code: This method is called on certain date filters for each new values

function preapreTooltip(chart) {
    var tickLength = chart.xAxis[0].tickPositions.length,
        ticks = chart.xAxis[0].ticks,
        tickPositions = chart.xAxis[0].tickPositions;
    for ( var iCntr = 0; iCntr < tickLength; iCntr++) {
         var tickVal = tickPositions[iCntr];

    //.label or .mark or both
    (function(tickVal) { // Is this good practice to call function like this?
        ticks[tickVal].label
        .on('mouseover', function(event) { // Is this good practice to call function like this?
            var label = '', labelCnt=0;
            $(chart.series).each(function(nCntr, series) {
                //business logic for each series
            });

           // calling method to show values in a popup
        });

        ticks[tickVal].label.on('mouseout', function(event) { // Is this good practice to call function like this?
            try {
                hideWrapper(); // hides popup
            } catch (e) {
            // do nothing
            }
        });

    })(tickVal);
  }
}
  • 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-13T21:40:54+00:00Added an answer on June 13, 2026 at 9:40 pm

    Whilst there are browser specific issues that need to be avoided when writing large pure JavaScript projects, when using a library such as jQuery it should be assumed that the library’s design helps you avoid these problems. However, considering memory leaks are rather hard to track down, and each different version of a particular browser could behave differently – it is far better to know how to generally avoid memory leaks than being specific:

    1. If your code is being iterated many times, make sure the variables you are using can be discarded by garbage collection, and are not tied up in closure references.
    2. If your code is dealing with large data structures, make sure you have a way of removing or nullifying the data.
    3. If your code constructs many objects, functions and event listeners – it is always best to include some deconstructive code too.
    4. Try to avoid attaching javascript objects or functions to elements directly as an attribute – i.e. element.onclick = function(){}.
    5. If in doubt, always tidy up when your code is finished.

    You seem to believe that it is the way of calling a function that will have an effect on leaking, however it is always much more likely to be the content of those functions that could cause a problem.

    With your code above, my only suggestions would be:

    1. Whenever using event listeners try and find a way to reuse functions rather than creating one per element. This can be achieved by using event delegation (trapping the event on an ancestor/parent and delegating the reaction to the event.target), or coding a singular general function to deal with your elements in a relative way, most often relative to this or $(this).

    2. When needing to create many event handlers, it is usually best to store those event listeners as named functions so you can remove them again when you are finished. This would mean avoiding using anonymous functions as you are doing. However, if you know that it is only your code dealing with the DOM, you can fallback to using $(elements).unbind('click') to remove all click handlers (anonymous or not) applied using jQuery to the selected elements. If you do use this latter method however, it is definitely better to use jQuery’s event namespacing ability – so that you know you are only removing your events. i.e. $(elements).unbind('click.my_app');. This obviously means you do have to bind the events using $(elements).bind('click.my_app', function(){...});

    being more specific:

    auto calling an anonymous function

    (function(){
      /*
       running an anonymous function this way will never cause a memory
       leak because memory leaks (at least the ones we have control over) 
       require a variable reference getting caught in memory with the 
       JavaScript runtime still believing that the variable is in use, 
       when it isn't - meaning that it never gets garbage collected. 
       This construction has nothing to reference it, and so will be 
       forgotten the second it has been evaluated.
      */
    })();
    

    adding an anonymous event listener with jQuery:

    var really_large_variable = {/*Imagine lots of data here*/};
    
    $(element).click(function(){
      /*
       Whilst I will admit not having investigated to see how jQuery
       handles its event listeners onunload, I doubt if it is auto-
       matically unbinding them. This is because for most code they
       wont cause a problem, especially if only a few are in use. For
       larger projects though it is a good idea to create some beforeunload
       or unload handlers that delete data and unbind any event handling.
       The reason for this is not to protect against the reference of the
       function itself, but to make sure the references the function keeps
       alive are removed. This is all down to how JS scope works, if you
       have never read up on JavaScript scope... I suggest you do so.
    
       As an example however, this anonymous function has access to the
       `really_large_variable` above - and will prevent any garbage collection
       system from deleting the data contained in `really_large_variable`
       even if this function or any other code never makes use of it. 
       When the page unloads you would hope that the browser would be able
       to know to clear the memory involved, but you can't be 100% certain
       it will *(especially the likes of IE6/7)* - so it is always best
       to either make sure you set the contents of `really_large_variable` to null
       or make sure you remove your references to your closures/event listeners.
      */
    });
    

    tearDowns and deconstruction

    I’ve focused – with regard to my explanations – on when the page is no longer required and the user is navigating away. However the above becomes even more relevant in today’s world of ajaxed content and highly dynamic interfaces; GUIs that are constantly creating and trashing elements.

    If you are creating a dynamic javascript app, I cannot stress how important it is to have constructors with .tearDown or .deconstruct methods that are executed when the code is no longer required. These should step through large custom object constructs and nullify their content, as well as removing event listeners and elements that have been dynamically created and are no longer of use. You should also use jQuery’s empty method before replacing an element’s content – this can be better explained in their words:

    http://api.jquery.com/empty/

    To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves.

    If you want to remove elements without destroying their data or event handlers (so they can be re-added later), use .detach() instead.

    Not only does coding with tearDown methods force you to do so more tidily (i.e. making sure you to keep related code, events and elements namespaced together), it generally means you build code in a more modular fashion; which is obviously far better for future-proofing your app, for read-ability, and for anyone else who may take over your project at a later date.

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

Sidebar

Related Questions

I'm a jQuery newbie as of this morning, and this is the code I've
I am newbie to jQuery, can someone explain what this code does: $(#currency form).submit(function(e)
I'm a bit of a jQuery newbie, so forgive me if this seems a
Jquery newbie here =) so I've got a code that slides the menu divs
I'm a jQuery newbie. Here are two code snippets that move the h1 element
I'm a newbie with jquery and i've used this code for tabs <script type=text/javascript
This may be a basic question... but I am a newbie to jquery. I
Rails newbie here. I'm looking to learn a little bit about jQuery so I
Bit of a jquery Newbie here, so go easy. Basically I'm trying to plug
jQuery newbie here. I've looked at dozens of similar questions on this site, as

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.