Live data from Hacker News

The jQuery Divide:Understand where jQuery ends and JavaScript begins

slideshare.net

41–50 of 78 posts

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#41
post #14

Earlier quoted context omitted.

jQuery as commonly used leads, in my personal experience, to hard-to-maintain programs. Just like Ruby on Rails, or Perl. There is also anecdotal evidence that jQuery qua jQuery has some fundamental limits. I worked on a largish jQuery application, written by ninja badasses, and it creaked a lot more than it should have for its size. Comparable systems built on YUI seemed to have more structure. I actually went throu…

I worked on a largish jQuery application, written by ninja badasses I don't think those " ninja badasses " were as good as you're describing them. If they were, they would realize that jQuery is not a framework that structures your application, but just a neat and light library that provides syntactic sugar for DOM-manipulation + dealing with cross-browser issues. jQuery as commonly used leads, in my personal experie…

Well, you may have a different experience.

I've also noticed a new trend in complex applications, eg JavelinJS and Yahoo's "Minty" mail app, that moves away from jQuery's or YUI's model. They feature a centralized controller/arbiter and a separation of CSS classes and event markup. Instead of jQuery + MVC it's a little of both.

See also http://news.ycombinator.com/item?id=2430271

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#42
post #36
post #18

About time someone came out and said this, good job.

Said what? Did I miss it? He seemed to just stop midway. He implied there was a problem, showed some code, but never showed where the problem lies or what it was, and certainly never mentioned any solution. All I got was some vague "this isn't pretty" vibes.

I can't speak to what else you missed or didn't miss, but you do seem to have missed that he is a she, so ...

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#43
post #39
post #35

Earlier quoted context omitted.

> jQuery as commonly used leads, in my personal experience, to hard-to-maintain programs. People using jQuery improperly is not something wrong with jQuery. jQuery is perfectly fine for large web applications, you just need to organize your code properly, with something like a MVC and ORM layer.

One can always argue "you're doing it wrong". People say the same about ror and perl. But in my experience, even with a good mvc and object model, and world-class folks, complex applications in jquery tend to become hard to maintain. I believe part of the reason is the tight coupling of state and events to the DOM. I'm also very skeptical of the dozen-odd undocumented "extensions" to the selector language. They are u…

:first-of-type is valid CSS3, though.

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#44

Earlier quoted context omitted.

I don't want to appear snarky, but as you admit yourself ("I dont have any data on it") you don't know if creating a jQuery object is expensive or not (to be fair, I don't know either) - so why all the extra complexity if you don't know whether the thing you are trying to avoid (creating instance of jQuery during event handlers) actually causes problems or not?

Well given that a lot of the "pro" jQuery tips say that one should avoid rerunning the selector in favor of some sort of memoization: $('.class').doSomehting(); $('.class').doSomethingElse(); $('.class').thirdThing(); in favor of var ele = $('.class'); ele.doSomething(); ele.doSomethingElse(); ele.thirdThing(); I would only assume that re-querying with the jQuery object inside of event handlers has the same adverse e…

Interacting with the DOM is what's slow. It's especially slow if the native querySelectorAll method isn't available because any non-trivial selections necessitate iterating over the DOM tree. That's why you don't want to re-query a selection multiple times.

That's also why it's recommended that all selections start with an id. The size of the tree to iterate over can be quickly reduced using the native getElementByID method.

In the case of $(this), the DOM node is already passed to the event handler, so wrapping it in a jQuery object doesn't touch the DOM tree at all. Furthermore, I always cache it (var $this = $(this);) if I'm going to use it more than once.

Also, in your first example, you don't have to rewrap containers[index] in a jQuery object. All the elements in the selection are already wrapped.

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#45

Earlier quoted context omitted.

When I use jQuery (MooTools is my lib of choice) I find myself using it in ways that is very unlike the majority of jQuery that is seen around the web. I absolutely hate the plugin system simply because you cannot easily get the instance of the plugin an refer to it later. To answer this I use function constructors with the module pattern (its easy and doesnt require an extra lib to get going) to create my plugins. A…

>>allows you to select an item from collected without having to rewrap it with the jquery object I think the following does the same thing: var collected = $('a').map(function(element) {return $(element)})

Or even simpler:

    var collected = $('a').map($)
However, map as defined on jQuery objects takes a function that takes the index as the first argument, and the element as the second argument:

http://api.jquery.com/map/

So, what you actually want is:

    var collected = $('a').map(function(index, element) {return $(element)})
However, jQuery.map has the element as the first argument, and the index as the second argument:

http://api.jquery.com/jQuery.map/

I would consider this to be the more intuitive way round, although I'd prefer it if both maps were consistent. So, you could write:

    var collected = $.map($('a'), $)
which strikes me as the neatest version.

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#46

Earlier quoted context omitted.

I don't want to appear snarky, but as you admit yourself ("I dont have any data on it") you don't know if creating a jQuery object is expensive or not (to be fair, I don't know either) - so why all the extra complexity if you don't know whether the thing you are trying to avoid (creating instance of jQuery during event handlers) actually causes problems or not?

Well given that a lot of the "pro" jQuery tips say that one should avoid rerunning the selector in favor of some sort of memoization: $('.class').doSomehting(); $('.class').doSomethingElse(); $('.class').thirdThing(); in favor of var ele = $('.class'); ele.doSomething(); ele.doSomethingElse(); ele.thirdThing(); I would only assume that re-querying with the jQuery object inside of event handlers has the same adverse e…

Well, I think I'm going to spend some time tomorrow looking at the jQuery source code to find out what really does happen but what I suspect is that using a selector is going to be a lot more expensive than creating a jQuery instance directly from an element - presumably using a selector means querying the DOM for matching elements (which could be quite complex) rather than simply wrapping a supplied element in a new jQuery object.

So if you do have some complex selector then I can see that avoiding re-running it makes a lot of sense - for DRY as much as efficiency. However, in an event handler you've been handed the element you want to work with and all you are doing is wrapping it in jQuery to make it easier to work with.

Or something like that :-)

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#47
post #36
post #18

About time someone came out and said this, good job.

Said what? Did I miss it? He seemed to just stop midway. He implied there was a problem, showed some code, but never showed where the problem lies or what it was, and certainly never mentioned any solution. All I got was some vague "this isn't pretty" vibes.

'He' is a she: Rebecca Murphey

  http://twitter.com/#!/rmurphey
  http://www.rebeccamurphey.com/

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#48
post #39

Earlier quoted context omitted.

One can always argue "you're doing it wrong". People say the same about ror and perl. But in my experience, even with a good mvc and object model, and world-class folks, complex applications in jquery tend to become hard to maintain. I believe part of the reason is the tight coupling of state and events to the DOM. I'm also very skeptical of the dozen-odd undocumented "extensions" to the selector language. They are u…

:first-of-type is valid CSS3, though.

And :first-child is valid CSS2.1.

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#49
post #44

Earlier quoted context omitted.

Well given that a lot of the "pro" jQuery tips say that one should avoid rerunning the selector in favor of some sort of memoization: $('.class').doSomehting(); $('.class').doSomethingElse(); $('.class').thirdThing(); in favor of var ele = $('.class'); ele.doSomething(); ele.doSomethingElse(); ele.thirdThing(); I would only assume that re-querying with the jQuery object inside of event handlers has the same adverse e…

Interacting with the DOM is what's slow. It's especially slow if the native querySelectorAll method isn't available because any non-trivial selections necessitate iterating over the DOM tree. That's why you don't want to re-query a selection multiple times. That's also why it's recommended that all selections start with an id. The size of the tree to iterate over can be quickly reduced using the native getElementByID…

> Also, in your first example, you don't have to rewrap containers[index] in a jQuery object. All the elements in the selection are already wrapped.

Really? I think that is the whole point of this long comment thread -- that the elements in a collection arent already wrapped.

http://jsfiddle.net/8nqtA/

This doesnt work unless i wrap a[i] in $()

But like you're saying, it may be fruitless since the dom is the bottleneck and we're bypassing it by already having the element.

Re: The jQuery Divide:Understand where jQuery ends and JavaScript begins

#50

Earlier quoted context omitted.

I don't want to appear snarky, but as you admit yourself ("I dont have any data on it") you don't know if creating a jQuery object is expensive or not (to be fair, I don't know either) - so why all the extra complexity if you don't know whether the thing you are trying to avoid (creating instance of jQuery during event handlers) actually causes problems or not?

Well given that a lot of the "pro" jQuery tips say that one should avoid rerunning the selector in favor of some sort of memoization: $('.class').doSomehting(); $('.class').doSomethingElse(); $('.class').thirdThing(); in favor of var ele = $('.class'); ele.doSomething(); ele.doSomethingElse(); ele.thirdThing(); I would only assume that re-querying with the jQuery object inside of event handlers has the same adverse e…

>why is the practice of rerunning the same selector frowned upon?

Rerunning the same selector is frowned upon for (at least) 2 reasons:

1) It can make for more readable code.

Sometimes it makes more sense semantically to have a local variable that describes the role of an element in the particular block of code you are working on, rather than just what selector you are using to get at the element.

Also, it makes the code shorter and less complex, in your example "tabs" is shorter and easier to read than "$('a.tabs')" etc.

2) ...because it's a stupid simple optimization to make.

This goes in general for ANY JS variable that is not in local scope, is the property of an object, or is returned as the result of a function.

It's so easy to just cache it as a local variable, you should probably just do that once you are accessing it a few times.

Even that has the whiff of premature optimization, but it's so easy and has such a low impact (or even improvement) on readability, that it's no big deal.

What you're talking about is much harder to justify imho.

Re-running the same selector is an instance where you are re-running a function that will definitely return the same result that you just got.

Effectively it's the same as

    function get5(){ return 5; };
    var x = 4 + get5();
    var y = 35 + get5();

...etc etc...so obviously it's better to just cache the return value and save the work of running a function.

It's hard to say because your examples aren't entirely clear to me, but I don't think your version as is is any better, perhaps even worse in some respects.

$('a.tab') returns A jQuery object that has a context that is a collection of DOM elements.

Your map function (which has the arguments reversed fyi) return's an array of jQuery objects (plural); each with a context of a single DOM element.

So you're creating a bunch of new instances of jQuery objects to possibly save an insignificant amount of context lookup time (getting the context of a jQuery object is not as heavy as selector lookup).

Again in the each you're creating a new jQuery object for each tab element, even though it was already in one. A jQuery object which will stick around via the closure.

So this is all to save looking up the index each click event for the tab (btw, something like $(this).index() would probably be cleaner) which may or may not be worth it.

I can see where you're going, and think you've got the right idea, but I also think what you use really depends on the situation.

I would say just write clean and idiomatic code first...that should be the default.... and then if you hit problems you can start optimizing based on the situation.

If looking up the index and context each click really is a problem (could be in some situations) you could find a solution based on the situation using strategies like event delegation, strategic naming, caching the index, associating them in a data structure of some kind, of any combination thereof.

Post reply on HN