Detailed Explanation of Reading Speed Example of js Object

  • 2021-12-04 17:57:20
  • OfStack

1. Accessing literals and local variables is the fastest, while accessing array elements and object members is relatively slow. When accessing object members, search on the prototype chain just like scope chain 1.

2. If the members found are too deep in the prototype chain, the access speed will slow down.

Therefore, we should minimize the search times and nesting depth of object members.

Instances


 //  Object member lookup twice 
  function hasEitherClass(element, className1, className2) {
    return element.className === className1 || element.className === className2;
  }
  //  Optimization, if the variable does not change, you can use local variables to save the lookup 
  function hasEitherClass(element, className1, className2) {
    const currentClassName = element.className;
    return currentClassName === className1 || currentClassName === className2;
  }

Content extension:

js Object Operational Performance Issues

1 The longer the string, the more time it takes to use str + = "xxx" (almost exponentially).

When the array of objects only has 400 elements, the access time for the properties and methods of each element reaches
1/4 millisecond of each property or method! If an element has 10 attributes, it takes at least 1 second to traverse the array once, which is horrible

3 The operations of FileSystem, and particularly write, are almost proportional to the square of the length of the string to be written.

4 Do not use their own defined methods for string operations, especially substitution and search, comparison;

I don't have a good grasp of regular expressions. When I use custom functions, I find that in the traversal mentioned in 2) above,

Custom functions take 80% of the time!


Related articles: