Javascript and CSS best practices for better performance
webuiarchitect.com
webuiarchitect.com
Why, frameworks exist for a reason and keeps us from reinventing the wheel. Many times a contributor to a framework has labored to make his routine as efficient as possible on the broadest array of platforms. Many times we can forgo a lot of optimization work by using the framework rather than rolling our own.
$("#myid") vs document.getElementById("myid")
for example, $() supports a vast amount of functionality.
Frameworks are made to make development quicker and easier, but its at the cost of performance.
In any case, if you're concerned about performance, you should examine the source code of the framework anyways to see if it's doing anything unnecessary from what's required of your application.
But for internal sites, storing my framework with my app lets my local web server cache it, instead of forcing users to traverse the network and go outside our firewall to Google's CDN.
In some cases, yes, but I'd argue that in most cases probably not.
If I'm earning a savings of < 1ms by making one choice over the other, it is probably more time efficient for me to simply not worry about it.
> 8 But if you have multiple string concatenation, prefer pushing to an Array and then calling the join() method on it. This is particularly helpful while building HTML snippets.
Depends on too many factors (browser, number of segments, segment length etc) to a be rule. Most of the time, the performance between this and += is going to be more or less the same.
> 26 With AJAX, GET works faster than POST. So prefer GET unless request length is more than 2K; yes, you guessed it right - IE.
Use GET or POST when you're supposed to not based on how much information you think you'll be sending.
> 27. Go easy on scripted animations.
What's easy? What's hard?
> 30. Always fall back to core JavaScript whenever possible. Limit framework usage.
It would be more prudent to _know_ your framework, and know when to not use it. IE (even in 9!) still doesn't implement indexOf - I can roll my own solution or use jQuery's inArray.
Here's a better article http://www.slideshare.net/nzakas/speed-up-your-javascript
<div class='table'>
<div class='tr'>
<div class='td'>1</div><div class='td'>2</div><div class='td'>3</div>
</div>
<div class='tr'>
<div class='td'>a</div><div class='td'>b</div><div class='td'>c</div>
</div>
</div>
Which is what DIV/FLOAT layout usually ends up becoming. If your DIV/FLOAT layout doesn't map to a structure like that, you're probably doing something wrong!Finally, has IE fixed the "we double the padding on floats" issue? Because float-driven layout with that bug is pretty hard, since IE will double your paddings while the other browsers don't.
I do know that proudly espousing TABLE in a job interview can cost you the job. But aside from that TABLE is very effective for most layouts. I do avoid TABLE inside TABLE of course, that's a code smell, but top level TABLE is the right tool most of the time. Do anti-TABLE people also avoid Excel?
Also, some things are pretty pointless (e.g. "#6 - no globals" doesn't do anything to improve performance), and other are poorly explained (e.g. "#13 - store local refs to out-of-scope vars" - what does that even mean?)
He even has syntax errors (e.g. "#7 - var fullName +=", really?) and other oddities ("#28 - element.bind" What in the world is that?)
The good points are largely regurgitation of stuff that is much better covered by Steve Souders, the Opera blog and other resources.
My test case: http://dqd.com/~mayoff/timeDom.html
That doesn't even make sense. The for-loop block doesn't create a new scope, so it's perfectly valid to refer to the variable `a`. Plus, even if you create a new scope, you can still refer to `a` due to closure.
For example, if you ran the code below in FireBug you'll get the same results (minor variance aside).
function foo() { var a = 0; for (var i=0, j=a; i<10000000; i++) { var x = j+i; } }
function bar() { var a = 0; for (var i=0; i<10000000; i++) { var x = a+i; } }
(function() { var start = new Date().getTime(), end; foo(); end = new Date().getTime(); console.log(end-start); })();
(function() { var start = new Date().getTime(), end; bar(); end = new Date().getTime(); console.log(end-start); })();
In your examples, just change the value of variable 'a' to 100 or 1000 and you will see the difference.
In fact, Crockford has mentioned this as well: http://javascript.crockford.com/code.html
"JavaScript does not have block scope, so defining variables in blocks can confuse programmers who are experienced with other C family languages."
Which is why this code works:
(function() {
for (var i=0; i<100; i++);
alert(i);
})();Notice the variable `i` is accessible from outside the for-loop.
Another thing, JavaScript 1.7 has introduced block level scopes.. you can achieve that by using the 'let' keyword instead of 'var' to declare a variable. Although most browsers don't support it yet.
ActionController::Base.asset_host = Proc.new { |source| "assets#{source.hash % 4}.example.com" }
(Why the heck do the kindergarten people of HN downvote this? It's a perfectly sensible question.)
the only one that is specifically says "yes for ie"
My favorite JS engine is the one implemented in the Evo browser. Among other things, it lacks support for arrays and for loops which makes development a tad interesting. And then we have the iPanel browser built for the Sunniwell set-top-box, in which you have no try/catch. Also, if you use more than five chained if/else, the browser will crash. However, this could be viewed as enforcing general programming best practices :)
it lacks support for arrays and for loops
Wow. They still call it Javascript?Could you please point at the docs, to see what kind of language they offer? Thanks!