3

Possible Duplicates:
jQuery Standards and Best Practice
Javascript Best Practices

After finding that a website I wrote is leaking memory like crazy, I've started trying to improve the way I write my Javascript/jQuery code.

For instance, instead of writing:

if ($('#elem').is(':checked'))

I can write:

if ($('#elem')[0].checked))

Directly interacting with the DOM rather than using jQuery as the middle man improves on speed, right?

As for memory leaks, should I consider jQuery callbacks to be like closures? If I reference an element in the callback, should I nullify the reference at the end of it's use? Or will the browser take care of that for me?

I'm just after some good tips to keep in mind when writing my code.

Community
  • 1
  • 1
dave
  • 7,717
  • 19
  • 68
  • 100
  • 1
    possible duplicate of [jQuery Standards and Best Practice](http://stackoverflow.com/questions/1245598/jquery-standards-and-best-practice), [Javascript Best Practices](http://stackoverflow.com/questions/39691/javascript-best-practices), [Object Oriented Javascript best practices?](http://stackoverflow.com/questions/907225/object-oriented-javascript-best-practices),... – sth Aug 18 '10 at 03:04
  • I'll take a look at those threads now. Thanks for the links. – dave Aug 18 '10 at 03:11
  • Of course the 2nd example is faster than the first. Anyways. For good practices are you talking about speed or readability? – Warty Aug 18 '10 at 03:30
  • Speed. I'll be the person handling the code so I don't have to worry about someone else getting confused trying to follow what it's doing. – dave Aug 18 '10 at 03:34

1 Answers1

0

Run all your JS through JSLint

Larry K
  • 47,808
  • 15
  • 87
  • 140