#192 ✓invalid
Douwe M.

$() with more than one argument

Reported by Douwe M. | February 16th, 2010 @ 11:25 AM

Allow us to use the $() function with more than one argument, so $('foo', 'bar', 'baz') would return an array, the same as $$('#foo, #bar, #baz').

Comments and changes to this ticket

  • MadRabbit

    MadRabbit February 16th, 2010 @ 06:03 PM

    the $ method is a fast version of $$('#foo')[0], it was specifically made to deal with the majority of cases, when you refer to just one node. So you kinda offer to reverse the optimization backward, coz this feature will slow the method down.

    and then, IMHO $$('#foo, #boo, #hoo') is more readable and expressive, than the version with several arguments.

  • Douwe M.

    Douwe M. February 16th, 2010 @ 06:12 PM

    Well, $() with multiple arguments would just have to loop through the arguments, and run document.getElementById() on every one of them. That would only be arguments.length times as slow as $() with only one argument, and it won't be (noticeably) slower when using only one argument, like it works now.
    IMO it makes sense to use $() when picking an element id, and using $$() when I want to use an often more complex CSS selector.

  • MadRabbit

    MadRabbit February 16th, 2010 @ 06:28 PM

    I understand your point, bur the $() method is not just finds an element by an id, it also extends elements under IE7,6 browsers, and it will also be used to access elements functionality when it will come to the safe-mode implementation. Your changes might have a small impact by itself, but as the method is called repeatedly in many places, it is crucial to keep it as small and fast as possible.

    Here. You can do the trick in a tough way like that. :)

    $w('foo boo hoo').map($);
    
  • Douwe M.

    Douwe M. February 16th, 2010 @ 06:32 PM

    But if $() would just loop through the arguments, and return one node when arguments.length == 1, and an array when it's more, the only extra code would be a loop that wouldn't even be used in most cases, and a check for the arguments.length... I can see your point, but I just don't really see how this would slow it down so much...

    Haha, nice one. I think I'll just continue using $$('#foo, #bar, #baz') then :)

  • MadRabbit

    MadRabbit February 16th, 2010 @ 07:04 PM

    • State changed from “new” to “invalid”

    because the complexity of algorithms gets multiplied, visiting every element in an array has a linear complexity O(n), visiting every item in an array of arrays, is square O(n^2). Changing one item processing to an array, might not a big deal for a simple function, especially if you deal with just a few items, but it will shift the complexity level for another algorithms that use this method.

    if you need this feature badly, RightJS is an open-box and you always can redefine the method for your app with just a couple of lines of code.

  • Douwe M.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

RightJS Core Tickets

Shared Ticket Bins

People watching this ticket

Pages