High performance JavaScript algorithm and process control

Time:2020-6-29
  • Performance is equivalent to while, do
  • Avoid using for in loops, = = unless traversing an object with unknown attribute quantity==

Es5: for in traverses not only arrays, but also objects.

Reason: each iteration of for in will search instance or prototype properties at the same time, and each iteration of for in loop will generate more overhead, so it is slower than other loop types, and the speed is generally 1 / 7 of other loop types. Therefore, avoid using a for in loop unless you explicitly need to iterate over an object with an unknown number of attributes. If you need to traverse a limited list of known attributes, it’s faster to use other loops

  • Foreach is not as good as for

Cannot break a loop (use break or return)

In for loops, you can use continue and break to control loops and jump out of loops, which foreach does not have. [in this case, from a performance point of view, for has an advantage over foreach. (for a data with a length of 100, when the for loop reaches 35, the function will be realized. If you achieve the purpose, you can break out of the loop; if you use foreach, you cannot exit the loop itself.) ]

  • for-of

ES6 introduces a new loop method, which is for of loop. It is simpler than the traditional for loop, and makes up for the short board of foreach and for in loops.

  • Efficiency comparison between switch and if else

The use of if else or switch is based on the number of test conditions: a large number of conditions tend to use switch instead of if else. This usually comes down to the readability of the code. If else is easier to read when there are fewer conditions, and switch is easier to read when there are more conditions.

The goal of optimizing if else is always to minimize the number of conditional bodies before finding the right branch. The simplest optimization method is to put the most common condition body first.

  • When there are many judgment conditions, the search algorithm, eg, binary search, is better than switch and if else
  • Memoisation (often used recursively)

An optimization technique used to speed up a computer program by storing the results of expensive function calls and return cached results when the same input occurs again.

Memoization is a technique in JavaScript that accelerates time-consuming lookup operations by caching results and reusing the cache in the next operation.

Here, memoization usually reduces execution time and affects the performance of our application. Memoization works best when we know that a set of inputs will produce an output.
Following best practice, memoization should be implemented on pure functions. Pure functions return whatever they input, without side effects.
Remember this is = = trade space for speed = =, so it’s best to make sure you’re worth it, some scenarios are necessary.
==Memory is the most efficient when dealing with recursive functions, which are used to perform heavy operations such as GUI rendering, Sprite and animation physics. = =