A little performance improvement for JavaScript!
This article lists 24 suggestions that make your code writing process easier and more efficient. Maybe you are still a beginner in JavaScript and have just finished writing your Hello World. There are many tips that will be very useful for your work. Maybe you know some tips, let's take a quick look and see if we can find something new!
1. Replace = with =
There are two different equal operators in JavaScript: ===|! ==And ==|! =. In contrast, the former is more recommendable. Use the former as much as possible.
"If two comparison objects share the same type and value, = true is returned ,! = False is returned ."
-JavaScript: The Good Parts
However, if the = and! =, You may encounter unexpected problems when operating on different data types. Before equality determination, JavaScript tries to convert them into strings, numbers, or Boolean values.
2. Avoid using Eval Functions
The Eval function uses a string as a parameter and executes the string as a JavaScript statement to return the result.
This function not only reduces the execution efficiency of your script, but also greatly increases the security risk, because it gives too much rights as text parameters. Never use it!
3. Do not use Quick writing
Technically, you can omit most of the curly arc and semi-colon. Most browsers can correctly execute the following statements:
- if(someVariableExists)
- x = false
However, if so:
- if(someVariableExists)
- x = false
- anotherFunctionCall();
You may think it is equal to the following statement:
- if(someVariableExists) {
- x = false;
- anotherFunctionCall();
- }
Unfortunately, this is not the case. The reality is that it is equivalent:
- if(someVariableExists) {
- x = false;
- }
- anotherFunctionCall();
As you noted, the beautiful indentation cannot replace this gorgeous curly arc. In all cases, please clearly write curly braces and semicolons. It can be omitted occasionally when there is only one line of statements, although this is not recommended below:
- if(2 + 2 === 4) return 'nicely done';
Think about the future, child.
Suppose that you need to add more commands for this if statement in the future development process? Why don't you add brackets?
4. Make good use of JS Lint
JSLint is a debugger written by Douglas Crockford. You only need to paste your code to quickly scan for any obvious errors and problems.
"JSLint scans the received code. Locate the problem, describe the problem, and give the approximate location in the source code. Possible problems include but are not limited to syntax errors, although syntax errors are indeed the most common. JSLint also checks the formatting style and structure errors of the Code with conventions. Scanning through JSLint does not guarantee that your program is completely correct. It just provides you with an extra pair of eyes for error detection ."
-JSLint document
Before you complete the code, put it in JSLint and check it to quickly eliminate your unintentional experience.