-
Recent Posts
Tags
app engines attributes cgi database DBI Debian frameworks full screen terminal GWT hacks hardware hobbit howto html inheritance javascript jQuery libraries Linux mac oracle OSX parallels Perl photoshop pixels PostgreSQL programming qa regression testing screen size Software Development style template toolkit term Theory translation type cast User Interface virtual machine virtual machines visual studio vmware web design web programming
dont fetch the count in loops repeatedly!
This should have been really obvious, but I haven’t really thought about what happens when you do
with respect to the function total(), which gets called over and over again. If that function has to search through a large document, and total is large, then this could be really inneficient and slow. The right way (obviously) to do this is to assign total() into a variable first, so the code becomes:
Here is an example that prints “checking count” 5 times:
<script type="text/javascript" src="jquery-1.4.2.js"></script> <script type="text/javascript"> function count() { $("#log").append("checking count<br>"); return 5; } $(function() { for (var i=0; i < count(); i++) { //do something } }); </script>