In today’s web programming many Java developers write JavaScript coding hence they tend to follow Java best practice and coding style in JavaScript as well. But there is huge amount of difference in both the coding style and best practices.
Lets take a simple example of looping an array in Java.
String perfArray[] = new String[1000]; //style 1 for (int i=0; i < perfArray.length; i++) { // do something } //style 2 int arrayLength = perfArray.length; for (int i=0; i < arrayLength; i++) { // do something }
As shown above both the styles are acceptable and work well. The style 1 is coding is better readability and maintainability compares to style 2. Both the style of code gives the same performance when executing in JVM.
Let take a similar example for JavaScript coding:
var perfArray = new Array(); //style 1 for (var i=0; i < perfArray.length; i++) { // do something } //style 2 var arrayLength = perfArray.length; for (var i=0; i < arrayLength; i++) { // do something }
The style 2 code performance is much better than style 1 in Internet Explorer 8 and Google Chrome browsers. In Mozilla Firefox it is not much difference.
Be cautious if your application contains many JavaScript code.
Leave a comment