Detailed example of reading speed of js objects

Detailed example of reading speed of js objects

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

2. If the member found is too deep in the prototype chain, access speed will be slow.

So try to minimize the number of searches and nesting depth of object members.

Examples

 // Perform two object member searches function hasEitherClass(element, className1, className2) {
    return element.className === className1 || element.className === className2;
  }
  //Optimization, if the variable does not change, you can use a local variable to save the search content function hasEitherClass(element, className1, className2) {
    const currentClassName = element.className;
    return currentClassName === className1 || currentClassName === className2;
  }

Content extension:

js object operation performance issues

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

2 When the object array has only 400 elements, the access time to the properties and methods of each element reaches 1/4 millisecond for each property or method! If an element has 10 attributes, then a traversal of the array will take at least 1 second, which is terrible.

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

4 Do not use your own methods to perform string operations, especially replacement, search, and comparison;

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

The custom function takes up to 80% of the total time!

This concludes this article on the detailed example of js object reading speed. For more information about js object reading speed, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Analysis of 4 implementation methods of JavaScript pre-parsing
  • js scope, scope chain and pre-parsing
  • Detailed explanation of JS scope and pre-parsing mechanism
  • JavaScript object-oriented implementation of magnifying glass case
  • 5 commonly used objects in JavaScript
  • JavaScript pre-analysis, object details

<<:  Detailed tutorial on installing Hbase 2.3.5 on Vmware + Ubuntu18.04

>>:  Web developers are concerned about the coexistence of IE7 and IE8

Recommend

How to build SFTP server and image server on Linux cloud server

First of all, you can understand the difference b...

In-depth analysis of MySQL explain usage and results

Preface In daily work, we sometimes run slow quer...

How to quickly modify the host attribute of a MySQL user

When you log in to MySQL remotely, the account yo...

4 ways to implement routing transition effects in Vue

Vue router transitions are a quick and easy way t...

An article to understand Linux disks and disk partitions

Preface All hardware devices in the Linux system ...

Vue's Render function

Table of contents 1. Nodes, trees, and virtual DO...

Several scenarios for using the Nginx Rewrite module

Application scenario 1: Domain name-based redirec...

Two solutions for Vue package upload server refresh 404 problem

1: nginx server solution, modify the .conf config...

A useful mobile scrolling plugin BetterScroll

Table of contents Make scrolling smoother BetterS...

Bootstrap3.0 study notes table related

This article mainly explains tables, which are no...

How to prevent Flash from covering HTML div elements

Today when I was writing a flash advertising code,...

Instance method for mysql string concatenation and setting null value

#String concatenation concat(s1,s2); concatenate ...

Time zone issues with Django deployed in Docker container

Table of contents Time zone configuration in Djan...