Avoid Unnecessary Variables
redundantPrintStatement contains a
String, but after
document.getElementById statement, we do you need that variable. It means you have uselessly declared the variable for just one String. The better way is as follow:
In the above snippet, have you noticed that is the problem with the for-loop in terms of optimization? If not, let me explain!
First I have declared an Array named
myArray, and after that I wrote down the loop, which would run five times, as the length of the array is five defined by
myArray.length. The problem with the for-loop is that it actually calls
myArray.length five times. Every time, whenever, the for-loop checks the condition, it calls
myArray.length. Imagine if your loop run a thousand or even ten thousand times. Although you should not go for that big of a loop, but still if you have to run that many times, you are dead in the water. Your code, not you I mean. The correct way is:
.js file is that the browser caches the file first time, and if somehow you reload the page, browser will check the cache. If that file exists in that cache, you will get result instantly instead of loading the complete file again.
Use Primitive Functions
I have discussed the most common mistakes that the novice programmers commit without thinking about their repercussions. The list is of course in complete, and there are more than fifty mistakes I can mentions here, but for now make sure you use the loops, variables, primitive types and
.js files optimally. That's it for this article!