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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:58:52+00:00 2026-06-04T13:58:52+00:00

I have some library code that is cycling endlessly on me. I’m not clear

  • 0

I have some library code that is cycling endlessly on me.

I’m not clear on how to best perform cycle detection and avoidance in javascript.
i.e. there’s no programmatic way of inspecting whether an object came from a “this” reference, is there?

Here’s the code.
Thanks!

setAttrs: function(config) {
    var go = Kinetic.GlobalObject;
    var that = this;

    // set properties from config
    if(config !== undefined) {
        function setAttrs(obj, c) {
            for(var key in c) {
                var val = c[key];

                /*
                 * if property is an object, then add an empty object
                 * to the node and then traverse
                 */
                if(go._isObject(val) && !go._isArray(val) && !go._isElement(val)) {
                    if(obj[key] === undefined) {
                        obj[key] = {};
                    }
                    setAttrs(obj[key], val);  // <--- offending code; 
                                              // one of my "val"s is a "this" reference
                                              // to an enclosing object
                }
  • 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-04T13:58:53+00:00Added an answer on June 4, 2026 at 1:58 pm

    The “reliable and clean” way I know of to deal with this situation is to use a collection of “visited” objects and then react — terminate, insert symbolic reference, etc — based on if the current object has already been “visited” or not.

    Mr. Crockford uses this approach in cycle.js and he uses an Array for the collection. Excerpt:

    // If the value is an object or array, look to see if we have already
    // encountered it. If so, return a $ref/path object. This is a hard way,
    // linear search that will get slower as the number of unique objects grows.
    
    for (i = 0; i < objects.length; i += 1) {
        if (objects[i] === value) {
            return {$ref: paths[i]};
        }
    }
    

    It is unfortunately not possible to use a primitive “Hash” approach for this in JavaScript because it lacks an Identity-Map. While the Array-collection bounds are O(n^2) this is not as bad as it sounds:

    This is because, if the “visited” collection is just a guard then the value of n is just the depth of the stack: only cycles are of importance while copying the same object multiple times is not. That is, the objects in the “visited” collection can be pruned on stack-unwind.

    In the cycle.js code the “visited” collection cannot be pruned because it must ensure the same symbolic name for a given object is always used which allows the serialization to “maintain the references” when it is restored. However, even in this case, n is only the number of unique non-primitive values traversed.

    The only other method I can think of would require adding a “visited property” directly to the objects being traversed, which I would consider a generally undesirable feature. (However, see Bergi’s comment about this artifact being [relatively] easily cleaned up.)

    Happy coding.

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

Sidebar

Related Questions

I have some library code (I cannot not change the source code) that returns
I have some code that creates a batch using the pyglet library. The whole
I have some code that copies all the directories from /Documents to Library/Caches/. The
In some library code, I have a List that can contain 50,000 items or
I have written some code that makes use of an open source library to
I have to call some Java library code that returns an untyped java.util.List and
I have some PHP code that calls into the cURL library. I'm using it
I am wondering about CI best practices I need to have some code that
I have some code that is using the PHP cURL library to upload some
I have some code shared among multiple projects in a static library. Even with

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.