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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:07:16+00:00 2026-06-01T08:07:16+00:00

I have read an why it’s better and how it’s implemented . But what

  • 0

I have read an why it’s better and how it’s implemented. But what i don’t really understand is how does it break the circular reference?.

how does it break the reference circle?

$(div1).data('item', div2);
$(div2).data('item', div1);

like example, the divs above point to each other, how is it prevented? I have a hunch, but i just want to make sure if my hunch is right.

  • 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-01T08:07:18+00:00Added an answer on June 1, 2026 at 8:07 am

    The circular reference problem happens in some browsers when you put a reference to a DOM object on a DOM object as a property on that DOM object. Then, you have two DOM objects pointing at each other. Removing a DOM object with a custom property on it doesn’t clear that custom property. A garbage collector that isn’t that smart doesn’t realize that this DOM reference doesn’t count so it gets stuck and there are several ways that this can lead to leaks.

    .data() solves this problem because the .data() data is NOT on the DOM object. It’s just a javascript data structure that can be associated with the DOM object via a unique string ID.

    The one confusing part of this is that when you read with .data("key") and the key isn’t found in the javascript .data() data structure, then and only then, jQuery will look for an attribute on the DOM object called "data-key". But whenever you write with .data("key", "myData"), it never writes to the DOM object, only to the javascript data structure.

    Thus, since .data() never writes the data to the DOM object, there can’t be any of these types of circular references that some browsers have trouble with.

    There are some other useful things to know about the .data() data structure. When you use jQuery’s .remove() to remove elements from the DOM or when you call $(elem).html("new html"), jQuery clears the .data() data on any removed items. This is one case where it’s good not to mix jQuery with plain javascript. If you’re using .data(), then you should always remove items from the DOM using jQuery functions so .data() is cleaned up appropriately. Otherwise, you can get memory leaks this way (both the .data() data can leak and any removed DOM objects that are referenced in the .data() can leak. But, if you only use jQuery methods for removing items from the DOM (including the replacing of innerHTML), then jQuery will clean things up appropriately and there will be no leaks.

    So, for example, this will create a memory leak:

    // suppose elem is a DOM element reference
    
    // store some data in jQuery's data storage on behalf of a DOM element
    $(elem).data("someKey", "someValue");
    
    // remove DOM element with plain Javascript
    elem.parentNode.removeChild(elem);
    

    Because you removed the DOM element with plain Javascript, jQuery did not have a chance to clean up the data you previously stored. The DOM element itself will be garbage collected, but the .data() value you previously stored is now orphaned in jQuery’s storage and is essentially a “leak” as it will likely never be cleared. On the other hand, if you do this:

    $(elem).data("someKey", "someValue");
    $(elem).remove();
    

    Then, jQuery will see that you’re removing the DOM element and will also clear the data you stored with .data().

    A fairly simple way to see how it works is to create a couple line script with a non-minimized version of jQuery and then just step through a call to $(elem).data("key", "whatever") in the debugger and watch how it works.

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

Sidebar

Related Questions

I have read some threads about this already but I don't understand the code,
I have read that you can do it, but would this really improve performance
I have read a lot about projective geometry and cross ratio, but I don´t
I have read this question but it's not quite what I was looking for.
I have read that gwt-ext is slow and it seems too bulky. How does
I have read several post on both matters but I haven't seen anyone comparing
I have read the MSDN article on MVVM and I am not really convinced.
I have read some topic regarding with my question but there code doesn't work
I have read some questions on this around here but my tries at the
I have read all the previous posts about this exact topic, but I still

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.