Below are some tips I have summarized for reference only.
The following code can be basically seen in the source code of jQuery. If there is anything wrong, please point it out.
Try to use the source method
JavaScript is an interpreted language and is slower to execute than a compiled language. Don't implement the method that the browser has implemented again. In addition, the methods that the browser has implemented have made a lot of optimizations in terms of algorithms.
Avoid global searches
In a function, global object storage is used as local variables to reduce global searches, because accessing local variables is faster than accessing global variables.
Minimize cycle times
With less loop, you can improve performance several times. If you want to do multiple operations on each element of an array, use one loop, multiple operations, instead of multiple loops, perform one operation per loop. Especially when making multiple regular matches, merge regular expressions as much as possible and find the corresponding matches in a traversal.
cycle
switch
Conditional branch
Branching the condition, arranged from high to low in order of possibility: it can reduce the number of times the interpreter detects the condition.
When branches with the same condition > 2 condition, using switch is better than if: switch branch selection is more efficient than if, which is particularly obvious in IE. For tests of 4 branches, the execution time of switch under IE is about half of if.
Use the trigonometric operator instead of the conditional branch.
Timer
If it is for running code, you should not use setTimeout, but setInterval, because setTimeout will initialize a timer every time, and setInterval will only initialize a timer at the beginning.
Another way to create objects - don't use new
Use boolean types whenever possible when used as tagged variables
Use true and false as markers directly, and do not use 1 and 0 of numbers or strings to markers.
The above JavaScript code performance optimization summary (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.