Loading ...
Sorry, an error occurred while loading the content.
 

What to store...

Expand Messages
  • Ruth Lang
    Hi, I have a question concerning Javascript and the DOM: For example I want to highlight an element when moving the mouse over it. Does it make more sense to
    Message 1 of 2 , May 8, 2009
      Hi,

      I have a question concerning Javascript and the DOM:

      For example I want to highlight an element when moving the mouse over it.
      Does it make more sense to store the id of the element, or the element
      itself:

      My information so far are:
      - it takes time to make a query on the DOM (with getElementById), so I think
      it would make more sense to keep the element itself in storage
      - is there a difference on required space between storing the id (=string)
      and storing the element (=DOM object), or is there no difference at all?

      Thank you very much for your help.

      Ruth
    • Jeff Schiller
      I m not an expert in internal browser workings but since the browser already has to keep the DOM object in memory, it would make more sense to store a
      Message 2 of 2 , May 8, 2009
        I'm not an expert in internal browser workings but since the browser already has to keep the DOM object in memory, it would make more sense to store a reference to the DOM object in your JS.

        I would suspect that the storage costs for a reference to a DOM object is the equivalent of a single C++ pointer (4 or 8 bytes depending on your architecture) while the storage costs for the ID string would be at least the size of a C++ pointer (4 or 8 bytes) and at most the length of the string.

        As you say, storing the DOM reference reduces query time.

        Regards,
        Jeff

        --- In svg-developers@yahoogroups.com, "Ruth Lang" <R.Lang@...> wrote:
        >
        > Hi,
        >
        > I have a question concerning Javascript and the DOM:
        >
        > For example I want to highlight an element when moving the mouse over it.
        > Does it make more sense to store the id of the element, or the element
        > itself:
        >
        > My information so far are:
        > - it takes time to make a query on the DOM (with getElementById), so I think
        > it would make more sense to keep the element itself in storage
        > - is there a difference on required space between storing the id (=string)
        > and storing the element (=DOM object), or is there no difference at all?
        >
        > Thank you very much for your help.
        >
        > Ruth
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.