Java vs. JavaScript coding

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.

Advertisements

30 Responses to Java vs. JavaScript coding

  1. Aditya says:

    Can you confirm the best option, you have chosen, to be best performant as well ?
    Agnostic of the language, if you write the for loop as follows
    for (int i=0; i < perfArray.length; i++)
    It would definitely hit the performance to some extent. Consider fetching the length of the array for every iteration, per say, in our example it's 1000.

  2. Venkat says:

    Yes, the style 2 is the best practice for all languages. The performance impact in Java is less (not worth to optimize), whereas in JavaScript is very high.

  3. Mohan Narayanaswamy says:

    Honestly, unless you are going to write map.google.com.. kind of application. I would advise not to focus these kind of tiny gains. Javascript engines should be smart enough to optimize.

    Focus where the bang for buck.

  4. AndrewFInk says:

    //Style 2.1

    String perfArray[] = new String[1000];
    for (int i=0, len=perfArray.length; i < len; i++) {
    // do something
    }

  5. Atalu says:

    Might want to check out EmEditor too. This is the tool I use for 99.99% of my development work. Its one of those thngis that once you gets used to something its hard to breakaway. I’ve been using EmEditor for over 8 years now and can’t seem to get used to anything else.

  6. google says:

    Great site you’ve got here.. It’s hard to find high quality writing like yours nowadays.
    I honestly appreciate people like you! Take care!!

  7. Pingback: Blackline Elite Review

  8. Alotspace says:

    How is the web reputation of the intended web host provider.
    They carryout pay per click method and use inbound and
    outbound links for getting more traffic per day. Choose wisely,
    do your research, and know many things about
    the company that you choose.

  9. Pingback: 7 minute paycheck

  10. Elliott says:

    I’m not sure where you are getting your info, but great
    topic. I needs to spend some time learning more or understanding more.

    Thanks for great information I was looking for
    this information for my mission.

  11. Hi I am so delighted I found your web site, I really found you by error, while I was searching on Google
    for something else, Regardless I am here now and would just like to say thank you for a marvelous post
    and a all round exciting blog (I also love the theme/design),
    I don’t have time to look over it all at the minute but I have book-marked it
    and also included your RSS feeds, so when I have time I
    will be back to read a lot more, Please do keep up the excellent jo.

  12. Nadine says:

    I just like the helpful info you supply in your articles.
    I will bookmark your blog and take a look at again here frequently.
    I’m quite sure I’ll learn plenty of new stuff proper here!
    Good luck for the next!

  13. Chat says:

    For example, Cisco has a couple of VPNs for smartphones – but that’s just the thing – you can only get them
    from one source – Cisco. For one, you only get access to some TV websites – a list will be
    provided on the website. Moreover, this app is very useful because it alerts you to get changes
    and flight delays once you have been issued a boarding pass.

  14. cheap viagra says:

    Good day! Do you use Twitter? I’d like to follow you if that would be okay.

    I’m undoubtedly enjoying your blog and look
    forward to new updates.

  15. Rosetta says:

    Hi there to all, the contents existing at this web site are in fact awesome for people knowledge, well,
    keep up the nice work fellows.

  16. If your medical doctor will not consider herpes a probability,
    they should explain you will want to. Humans are
    pure reservoir of herpes.

  17. regime says:

    I’m curious to find out what blog system you have been using?
    I’m having some small security issues with my latest site and I would like to find something more safeguarded.
    Do you have any suggestions?

  18. Good day! I know this is kind of off topic but
    I was wondering which blog platform are you using
    for this website? I’m getting tired of WordPress because I’ve had issues with hackers and I’m looking
    at alternatives for another platform. I would be fantastic if you could point me in the direction of a good platform.

  19. Tonia says:

    You can still get an excellent meal at a
    restaurant that doesn’t participate directly in the program, because the restaurants often have gluten-free pasta stashed away
    for their regular clients, and they’re already well familiar with celiac disease and gluten-free food.
    In principle you should always only buy the least processed rice.
    It was well known that oats were grown, stored, milled and packaged with wheat products are
    were typically very contaminated with gluten.

  20. Byron says:

    I’m impressed, I have to admit. Rarely do I come across a blog that’s both educative
    and amusing, and let me tell you, you’ve hit the nail on the head.
    The issue is something that too few men and women are speaking intelligently about.

    Now i’m very happy that I found this in my hunt for something regarding this.

  21. This site was… how do I say it? Relevant!!
    Finally I have found something which helped me. Thanks a lot!

  22. This is my first time pay a visit at here and i am truly impresseed to read everthing at one place.

  23. Suρerb, what a Ьlog it is! Thiѕ blog presents valuable
    information to us, keep it up.

  24. gen.tr says:

    FilingA business files the bankruptcy court approval
    by the major and minor leagues, Bud, limb who is qualified and
    possess the business owner, you should raise these numbers only.
    So, the company was able to reclaim the VATcode.
    In fact, she encourages limb them to be researched.

    But if you focus on the bleeding-edge of design thinking, or to prvide accurate figures.

    In my 32-years business career, you could use the following actions can impact your business to try to counteroffer?

  25. tomato pill says:

    Howdy! I’m at work surfing around your blog from my new iphone
    3gs! Just wanted to say I love reading through your blog and look forward to all your posts!
    Carry on the outstanding work!

  26. You could certainly see your skills within the work you write.

    The sector hopes for more passionate writers such
    as you who are not afraid to mention how they believe.
    At all times follow your heart.

  27. This piece of writing will assist the internewt users for setting up new blog
    or even a weblog from start to end.

  28. zenonia 5 hack says:

    What’s Taking place i am new to this, I stumbled upon this I have discovered
    It absolutely helpful and it has helped me out
    loads. I am hoping to give a contribution & assist other users like its aided me.
    Good job.

  29. Twitter Bot says:

    If you are going for best contents like me, just visit
    this site everyday because it offers feature contents, thanks

  30. ppfr.it says:

    of these types of mixtures has a wider synergic appearance, reducing the androgenic special effects while available muscle seed is rather extreme.
    Since Anadrol is definitely an oral steroid, it must be blended with
    injectable variety of further steroid for accomplishing bet
    final results, before too long. Simply because it
    contain men hormonal, primary allergic reactions from the
    work are currently concerning virilization.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: