Youtilize

Read all about technology, web development and creative entrepreneurship

Currently showing 2 posts tagged javascript

 

Jun 08, 2008
Web development

Scope chain pain

If you’re like me and use a lot of framework singletons, such as the popular YAHOO.util.Dom, there’s something you can do so speed things up.

Every time you use a global variable, the browser has to work its way up the scope chain until it reaches the global scope. By giving all those pesky global vars a local reference, you can significantly lower the time it takes to find that variable.

Remember, JavaScript code is executed on the end-user’s computer, so writing efficient code should always be the #1 priority. Here’s an example with using YUI core components which are accessed frequently:

(function() {

var lang     = YAHOO.lang,
    dom      = YAHOO.util.Dom,
    event    = YAHOO.util.Event;

var newObject = function() {
  // 'lang' is much faster to lookup than 'YAHOO.lang' and so on...
  alert(lang.JSON.stringify({"key" : "value"}));
};

})();

By writing your code within a self-executing function, variables lang, dom, and event are only local to that function and everything inside of it. So, lookups to such frequent tools like YAHOO.util.Dom are much faster.

DOM caching

Similarly, lowering the times you access the DOM improves the performance as well, most of the times drastically.

So if you have something along the lines of this (but hopefully more profound):

for (var i=0; i<100; i++) {
  document.getElementById('el').innerHTML = i;
}

Do this:

var el = document.getElementById('el');
for (var i=0; i<100; i++) {
  el.innerHTML = i;
}

In other words, only do one DOM lookup per element and save the reference to a variable.

That’s it. Happy coding.

 

Jun 08, 2008
Web development

... at least according to the TIOBE index, which measures language popularity based on the number of web searches:

The ratings are calculated by counting hits of the most popular search engines. The search query that is used is +”<language> programming” The search query is executed for the regular Google, Google Blogs, MSN, Yahoo!, and YouTube web search for the last 12 months. The web site Alexa.com has been used to determine the most popular search engines.

Observations

  • PHP moves up one spot from last year into #4 and still dominates the web languages
  • ActionScript enters top 20 (as #20) and I expect it to grow even more in the coming years
  • Java dominates with 20% market share and sits comfortably at the #1 spot
  • Pascal enters top 20 as well at #15, jumping up eight spots in one year! This one I’m just confused about, but I do miss the language
  • JavaScript and Ruby are #9 and #10 respectively, though over last 4 years, Ruby has risen tremendously (#27 in 2004) with the help of Ruby on Rails

View the rest