Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does omitting semicolons affect performance in JavaScript?

Tags:

I was discussing with a colleague about JavaScript while looking at some snippets. We noticed that these snippets were missing the ; at the end of the statements. We all know that JS is interpreted correctly even if no semicolon is shown at the end of a line, but I was wondering if this affects somehow the performance of evaluation, since it is an interpreted language.

like image 752
Masiar Avatar asked Jan 17 '13 13:01

Masiar


People also ask

Is it OK to not use semicolons in JavaScript?

Semicolons are an essential part of JavaScript code. They are read and used by the compiler to distinguish between separate statements so that statements do not leak into other parts of the code. The good news is that JavaScript includes an automatic semicolon feature.

Is it necessary to place a semicolon at the end of every statement in JavaScript?

Nope, in javascript, there is no need to write a ; at the end unless you are writing two lines of code in the same line, such as, console.


1 Answers

A javascript file with spaces, semi-colons and comments is heavier. That's the main impact.

But you're a coder and you have to maintain the code, so this very slight impact is much less important than the adverse one on readability. And omitting the semicolons means you know when you can omit them. But the rules aren't so simple and learning them isn't worth your time.

Leave the semi-colons where they are, you'll avoid bugs.

And use a minifier to build a more concise code for the browser if you want to have the lightest possible code. It's its duty, not yours.

like image 191
Denys Séguret Avatar answered Oct 18 '22 15:10

Denys Séguret