Click here to Skip to main content
15,891,431 members
Articles / Programming Languages / Javascript

JavaScript – Write Less Not Use Less

Rate me:
Please Sign up or sign in to vote.
4.40/5 (9 votes)
17 Jun 2016CPOL1 min read 8.6K   3   4
JavaScript - write less not use less

Whether it is Angular or knockout or any other client side library, in all my training, I always make one statement. “Write less JavaScript. Write less doesn’t mean use less”.

Let me explain it in a little more detail.

Unlike other programming languages, JavaScript is not a compiled language. Hence, it is very important how much code we write, it comes to JavaScript. Look at the following two code snippets.

Code Snippet 1

JavaScript
for(i=0;i<100;i++)
{
      if(flag==true)
{
PrintRandomNumberLessThanSpecifiedNumber(i);
}
else
{
            PrintRandomNumberGreaterThanSpecifiedNumber(i);
}
}

Code Snippet 2

JavaScript
if(flag==true)
{
      for(i=0;i<100;i++)
      {
            PrintRandomNumberLessThanSpecifiedNumber(i);
      }
}
else
{
      for(i=0;i<100;i++)
      {
            PrintRandomNumberGreaterThanSpecifiedNumber(i);
      }
}

Both will generate the same output, then which code is better?

The correct answer is:

  • If it is written using a language like C#, Java which supports compiler, then without any doubt, the second one is better because it will provide a better execution performance.
  • But if it is JavaScript, then the first code is better because it is smaller in size.

As I said before, JavaScript is not a compiled language. It’s an interpreted language. “JavaScript code will get downloaded as it is to the client machine”. It means “More we write, more will be downloaded and less performance”. Hence, while writing JavaScript, it’s very important to keep an eye on how much we write. “Less we write, better will be the performance”.

On the other hand, it doesn’t mean we should use less JavaScript. Use it wherever it is required but with the help of some custom logic and strategy, try to accomplish the requirement with less code.

Hence it’s said, “Write less JavaScript, not use less”

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Just Compile
India India
Learning is fun but teaching is awesome.

Who I am? Trainer + consultant + Developer/Architect + Director of Just Compile

My Company - Just Compile

I can be seen in, @sukeshmarla or Facebook

Comments and Discussions

 
SuggestionSome problems with this approach Pin
ahagel21-Jun-16 6:24
professionalahagel21-Jun-16 6:24 
QuestionIt's possible to do a lot better than that Pin
rinzai21-Jun-16 2:05
rinzai21-Jun-16 2:05 
GeneralUseless... Pin
jwjosefy17-Jun-16 4:43
jwjosefy17-Jun-16 4:43 
SuggestionWhat about.........conditional short cutting Pin
DaveAuld17-Jun-16 4:38
professionalDaveAuld17-Jun-16 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.