On Feb 20, 2:30 pm, sarapapa <dyama...@gmail.com> wrote:
> Hi all,
> I am using prototype.js for a few year.
> The issue of JavaScript performance of my Web application occurs
> frequently recently.
> When it examined a cause, it was to have used many $ method
> ("document.getElementById").
> I think that the performance of the $ method is improved dramatically
> if prototype.js cached an HTML element beforehand.
> (I can expect JavaScript performance up of several times in Web
> application.)

Caching DOM elements is a bit problematic due to their dynamic nature.
You have no idea when an element that's being stored in a cache is
permanently removed from the document. While `document.getElementById`
would return `null` for such "deleted" elements, caching mechanism
would simply return a cached element (the one that no longer exists in
a document). That DOM element will not be able to be garbage collected
as well, as there will always be a reference to it from wtihin cache.
While you can take some precautions to "work around" these issues, any
attempt to do so will most likely make cache-based implementation even
slower than the original `getElementById`:

A "naive" approach is clearly faster:

var getById = (function(){
  var c = { };
  return function(id) {
    return (c[id] || (c[id] = document.getElementById(id)));

But a more or less "smart" one is already slower:

var getById = (function(){
  var c = { };
  return function(id) {
    return (c[id] && c[id].parentNode !== null)
      ? c[id]
      : (c[id] = document.getElementById(id));

Element extension is very slow in JScript, so `$` is not slow due to
`getElementById` but is slow due to `Element.extend`. There are also
dozens of other things that Prototype does which cause JScript to fall
on its knees. Element.<method>(@element) notation, for example, is
much faster than @element.<method> one, since <method> is actually a
"wrapped" Element.<method> function - and so carries a burden of extra
abstraction layer. Try to use the former one when performance is at

You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to