Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7848 (closed enhancement: fixed)

Opened 1 year ago

Last modified 1 year ago

[PATCH] Optimized DOM navigation methods

Reported by: haraldmartin Assigned to: savetheclocktower
Priority: high Milestone: 1.x
Component: Prototype Version: edge
Severity: minor Keywords: DOM traversal discuss
Cc:

Description

The DOM navigation methods (#up #down #previous #next) are today a bit slower than they could be (even if the new Selector improved them a lot). I think a lot of people often use them without any arguments (at least I do as well as in script.aculo.us). Right now the methods will recursively collect all nodes before picking out the correct one which isn't necessary if we only want the first element.

This patch will check if any index or selector was given and if neither, just return the first element found.

My simple benchmarks page showed these results:

* more than 3.5 times faster on Firefox Mac

* ~2.7 times faster on Safari 2

* almost 5 times faster on Firefox Win

* about 1.5 times faster on IE 7

Attachments

optimized_dom_nav.diff (1.6 kB) - added by haraldmartin on 03/16/07 21:37:45.
updated.patch (2.2 kB) - added by haraldmartin on 04/16/07 13:21:23.
update2.patch (2.7 kB) - added by haraldmartin on 04/16/07 20:10:53.
Patch with anonymous findChildNodes instead

Change History

03/16/07 21:37:45 changed by haraldmartin

  • attachment optimized_dom_nav.diff added.

03/19/07 19:00:15 changed by mislav

  • owner changed from sam to savetheclocktower.

Thanks a lot! This seems really smart, and it isn't big at all

03/22/07 17:35:32 changed by mislav

  • keywords changed from DOM to DOM traversal discuss.

04/13/07 10:46:42 changed by madrobby

  • priority changed from normal to high.

+1 -- needs a bit of cleanup for down before adding it in, though

04/16/07 13:21:23 changed by haraldmartin

  • attachment updated.patch added.

04/16/07 13:23:26 changed by haraldmartin

I've refactored #immediateDescendants and #down to use a #findChildNodes method that will be used by both methods. Not sure if the name (findChildNodes) is the best so let me know if you have any comments.

04/16/07 14:56:32 changed by mislav

Seems nice. The only problem remains "descendants" vs. "findChildNodes" from the user perspective. The latter obviously being an internal method, it could possibly be made private?

04/16/07 19:09:13 changed by haraldmartin

Yes i was thinking on that as well. What about something like this

Element.addMethods((function() {
  function findChildNodes(element, single) {
    //...
  }
  return {
    down: function(element) {
      return findChildNodes(element, true);
    },
    
    immediateDescendants: function(element) {
      return findChildNodes(element, false);
    }
  };
})());

or did you have another solution in mind?

04/16/07 19:29:44 changed by mislav

Well, yeah. Unfortunately we don't yet practice this in the framework, but you're welcome to resubmit the patch with this kind of true privacy so we can compare the two and decide.

04/16/07 20:10:53 changed by haraldmartin

  • attachment update2.patch added.

Patch with anonymous findChildNodes instead

04/17/07 07:17:41 changed by haraldmartin

I meant "private" not anonymous..

04/24/07 08:21:17 changed by Tobie

  • status changed from new to closed.
  • resolution set to fixed.

Fixed in [6562]