7 Best Practices for Effective Javascript Performance & Security

 

Coding anything requires a lot more effort than many believe. The bigger the project, the more difficult to code; you have to take performance, sustainability, updatability, security and so much more into account.

JavaScript

Below, you can find out 7 KILLER tips for making sure your Javascript code is lean and bullet proof:

Performance

  • Try to place as many scripts as you can at the bottom of your document. If you have a script that’s only purpose is to add some sort of functionality, like when a button is clicked or a form is submitted, place it at the bottom. A website will load a Javascript file at the top before it renders the rest of the page, so it’s best to get the content to the user as quickly as possible.
  • Avoid placing static variables inside loops. If it doesn’t need to change as a loop runs, it shouldn’t be in the loop. Remember, anything inside a loop will run on each iteration, so try to keep your code trim and tidy.
  • According to Ricco Leung from Jask, a Birmingham-based web design firm“Instead of using New Array(), use []. You can simply list out items in array  like var a = ['John','Smith']; rather than declaring each item individually.”
  • Use a timer function (such as found in Firebug) to run a check on your code, and check for memory leaks and broken code that could potentially be slowing things down.
  • Always, always use semi-colons. Whilst modern browsers will probably let you get away without them, they can lead to issues that can cause problems later on. Imagine sifting through hundreds, if not thousands of lines of code fixing issues that arise.

Security

  • Do not use Eval. For those not familiar with the “eval” function gives access to JavaScript’s compiler. Essentially, we can execute a string’s result by passing it as a parameter of “eval”. Whilst this isn’t performance effective, it also acts as a security risk as you’re giving way too much control to the passed-in text.
  • For even more tips, we recommend reading the following article to brush up on Javascript Security and also unearth many useful tools that you may not have come across before:http://coding.smashingmagazine.com/2009/02/08/50-extremely-useful-javascript-tools/

Never jump headfirst into code, especially if what you’re looking to code is going to get complex. The key is in the planning… spend a small amount of time planning at the outset, and you could save much more time in patching problems later on.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>