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

Solution to the problem of not finding Tomcat configuration in Intelli Idea

I joined a new company these two days. The compan...

How to deploy services in Windows Server 2016 (Graphic Tutorial)

introduction Sometimes, if there are a large numb...

Float and Clear Float in Overview Page

1. Float: The main purpose is to achieve the effe...

The ultimate solution for writing bash scripts with nodejs

Table of contents Preface zx library $`command` c...

Does Mysql ALTER TABLE lock the table when adding fields?

Table of contents Before MySQL 5.6 After MySQL 5....

Design Reference Beautiful and Original Blog Design

All blogs listed below are original and uniquely ...

vue3.0+echarts realizes three-dimensional column chart

Preface: Vue3.0 implements echarts three-dimensio...

Summary of methods for finding and deleting duplicate data in MySQL tables

Sometimes we save a lot of duplicate data in the ...

Notes on using the blockquote tag

<br />Semanticization cannot be explained in...

How to expand the disk size of a virtual machine

After Vmvare sets the disk size of the virtual ma...

Detailed explanation of Linx awk introductory tutorial

Awk is an application for processing text files, ...

Analyze how a SQL query statement is executed in MySQL

Table of contents 1. Overview of MySQL Logical Ar...

How to block IP and IP range in Nginx

Written in front Nginx is not just a reverse prox...