Here’s My Javascript:
document.getElementById('post').style.color = "black";
document.documentElement.className = "active";
alert(document.documentElement.clientWidth);
Which of the following minified versions would you say is better? Why? They’re basically the same length but the second method uses an anonymous function to rename some variables.
Would there be a difference in speed (however many nanoseconds that would be)?
Normal minification:
document.getElementById('post').style.color="#000";document.documentElement.className="active";alert(document.documentElement.clientWidth);
or with an anonymous function…
(function(){var d=document,h=d.documentElement;d.getElementById('post').style.color="#000";h.className="active";alert(h.clientWidth)})();
The second one
(function(){var d=document,h=d.documentElement;d.getElementById('post').style.color="#000";h.className="active";alert(h.clientWidth)})();is better because it has in its local scope the global variables’ local copy and it is faster. This has performance advantages in garbage collection and scope chain walking.Ref (localized section)