-1

Are the following 2 pretty much the same, or one is better and why?

A)

$myLists.find(".panel").hide().end()
    .find(".tabs a.active").removeClass("active").end()
    .find($this.attr("href")).fadeIn(250);

B)

$myLists.find(".panel").hide();
$myLists.find(".tabs a.active").removeClass("active");
$myLists.find($this.attr("href")).fadeIn(250);
DimitrisK
  • 85
  • 1
  • 8

2 Answers2

1

I doubt there is any significant difference in the performance between the two. I would go for option B simply because I find it more 'readable' (even though truth be told in this particular case A is just as readable).

Marxley
  • 120
  • 1
  • 6
1

The difference in performance, if there is one, is outstandingly minuscule. It is not worth bothering about.

The only other difference there could be would be in ease of reading and writing the code. That question is really up to you. I find your chained code quite confusing. If I were to do it that way, I would do something like this:

$myLists
    .find(".panel")
        .hide()
        .end()
    .find(".tabs a.active")
        .removeClass("active")
        .end()
    .find($this.attr("href"))
        .fadeIn(250);

Indenting the code this way allows you to see precisely what elements you're working on at any given time. As I say, though, this is very much a matter of preference or your house coding style.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • I like your formatting, even though it gets pretty long (tall to be precise). Maybe, the code would be even better (a bit shorter and readable), if the .end()s were added to the lines above, like: .hide().end() – DimitrisK Nov 06 '13 at 11:48
  • or just move the first find in the example A, below myLists and keep it to 4 lines – DimitrisK Nov 06 '13 at 12:00
  • @DimitrisK As I say, it's up to you. Personally I prefer to have one function call per line. – lonesomeday Nov 06 '13 at 12:19