Javascript optimization method based on pre-compiling technique

Precompilation technology is used to perform search and replacement, for loop optimization, object conversion and other processing on JavaScript code, which solves the problem of insufficient optimization of the precompilation process in the existing technology, and achieves more efficient code optimization and stability improvement.

WO2024212609A1PCT designated stage expired Publication Date: 2024-10-17CHINA TELECOM CLOUD TECH CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
PCT/CN2023/143126
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-12-13
Filing Date
2023-12-29
Publication Date
2024-10-17

AI Technical Summary

Technical Problem

The pre-compilation process of the existing JavaScript engine during execution only involves the improvement of variable declarations and function declarations, and the optimization of the code is weak, and deeper optimization cannot be performed before the code is executed.

Method used

Using an optimization method based on precompilation technology, JavaScript code is precompiled through steps such as search and replacement, for loop optimization, conversion of variables and objects into literals, and on-site updates, including global variable replacement, with statement optimization, and object attributes. Replacement, for loop processing, constant expression calculation and if statement conversion improve the optimization effect before code execution.

Benefits of technology

It achieves more thorough code performance and efficiency optimization, reduces dependence on developers' coding habits and technical levels, and improves code stability and execution efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2023143126_17102024_PF_FP_ABST
    Figure CN2023143126_17102024_PF_FP_ABST
Patent Text Reader

Abstract

The present invention relates to the technical field of computers, and in particular to a JavaScript optimization method based on a pre-compiling technique. The method comprises: obtaining JavaScript code, carrying out search and replacement on the JavaScript code to obtain first optimized JavaScript code; performing for loop optimization on the first optimized JavaScript code to obtain second optimized JavaScript code; determining whether the second optimized JavaScript code has a plurality of consecutive variable declarations, and if the second optimized JavaScript code has a plurality of consecutive variable declarations, converting the plurality of consecutive variable declarations into a single statement declaration; searching for arrays and objects created using constructors in the second optimized JavaScript code, converting the arrays and the objects into literals, and setting the converted second optimized JavaScript code as third optimized JavaScript code; and performing on-the-spot updating on the third optimized JavaScript code to complete JavaScript optimization. The present invention can more thoroughly optimize code performance and efficiency, is less reliant on the coding habits and the technical skill of developers, and improves code stability.
Need to check novelty before this filing date? Find Prior Art

Description

A JavaScript optimization method based on precompilation technology Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a JavaScript optimization method based on precompilation technology. Background Art

[0002] In today's internet age, JavaScript has become one of the primary languages ​​for front-end development, used to develop a wide range of web and mobile applications. As the number of JavaScript applications continues to grow, so too does the size and complexity of JavaScript code. The efficiency and performance of this code are crucial to user experience and system stability.

[0003] Currently, to improve JavaScript code execution efficiency and performance, developers typically use the following optimization methods: 1. Improving code execution efficiency through best coding practices such as event delegation, modularization, componentization, and asynchronous programming. 2. Improving code execution efficiency through the use of HTTP 2.0, CDNs, and code compression and streamlining. 3. Performing code reviews to ensure code is standardized and well-written, thereby improving code execution efficiency. However, these methods are all optimizations performed during code development and rely heavily on the developer's coding habits and technical skills.

[0004] In addition, existing JavaScript engines (such as V8 and SpiderMonkey) optimize JavaScript execution efficiency during execution (such as through just-in-time compilation and inline caching). The precompilation process performed before executing the code only involves the improvement of variable declarations and function declarations, and the optimization of the code is relatively weak. Therefore, there is an urgent need for a JavaScript optimization method based on precompilation technology to achieve optimization before code execution.

[0005] Summary of the Invention

[0006] The purpose of the present invention is to provide a JavaScript optimization method based on precompilation technology to solve the problem that the precompilation process performed before executing the code only involves the improvement of variable declarations and function declarations, and the optimization of the code is relatively weak. It can realize precompilation optimization before code execution and improve code execution efficiency and performance.

[0007] The present invention provides a JavaScript optimization method based on precompilation technology, comprising:

[0008] Obtaining JavaScript code, performing search and replacement on the JavaScript code to obtain a first optimized JavaScript code;

[0009] Perform for loop optimization on the first optimized JavaScript code to obtain a second optimized JavaScript code;

[0010] determining whether the second optimized JavaScript code contains multiple consecutive variable declarations, and if so, converting the multiple consecutive variable declarations into a single statement declaration;

[0011] Finding arrays and objects created by using the constructor in the second optimized JavaScript code, converting the arrays and objects into literals, and setting the converted second optimized JavaScript code as the third optimized JavaScript code;

[0012] The third optimized JavaScript code is updated on-site to complete the optimization of JavaScript.

[0013] In some embodiments of the present application, searching and replacing the JavaScript code includes:

[0014] Searching for global variables in the JavaScript code and replacing the global variables;

[0015] Find the with statement in the JavaScript code and replace the with statement;

[0016] Find the object attributes in the JavaScript code and replace the object attributes.

[0017] In some embodiments of the present application, searching for global variables in the JavaScript code and replacing the global variables include:

[0018] Searching for a global variable in the JavaScript code, and if multiple references to the same global variable exist within the same scope, saving the global variable as a first local variable, and replacing references to the global variable with references to the first local variable.

[0019] In some embodiments of the present application, searching for a with statement in the JavaScript code and replacing the with statement includes:

[0020] Find a with statement in the JavaScript code, save the variable contained in the with statement as a second local variable, and replace the with statement with the second local variable.

[0021] In some embodiments of the present application, searching for object properties in the JavaScript code and replacing the object properties include:

[0022] Searching for an object property in the JavaScript code, and if multiple references to the same object property exist within the same scope, saving the object property as a third local variable, and replacing the references to the object property with references to the third local variable.

[0023] In some embodiments of the present application, before searching for the object properties in the JavaScript code, the method further includes:

[0024] Determine whether an array-like object exists in the JavaScript code; if so, replace property lookup of the array-like object with value lookup.

[0025] In some embodiments of the present application, performing for loop optimization on the first optimized JavaScript code includes:

[0026] Finding a for loop in the first optimized JavaScript code, determining the amount of data in a data set of the for loop, the time required to process the data set of the for loop, and the algorithmic complexity of the for loop;

[0027] If the data volume of the data set of the for loop exceeds a preset data volume, or the time for processing the data set of the for loop is greater than a preset time, or the algorithm complexity of the for loop is greater than O(n), then the data set of the for loop is determined to be a large data set, and the for loop is processed using a Duff device;

[0028] Otherwise, use the multiple function to process the for loop;

[0029] If the termination condition of the for loop is accessing the target object attribute, the object attribute is replaced.

[0030] In some embodiments of the present application, performing a localized update on the third optimized JavaScript code includes:

[0031] Find the appendChild call in the third optimized JavaScript code, and use document fragments to build a DOM structure to perform on-site update on the appendChild call.

[0032] In some embodiments of the present application, the method further comprises:

[0033] Determine whether the string of the JavaScript code is contained in eval, setTimeout, setInterval, or new Function. If so, embed the code corresponding to the string in the code of eval, setTimeout, or setInterval. If so, convert the new Function into a general function.

[0034] In some embodiments of the present application, the method further comprises:

[0035] Search for a constant expression in the third optimized JavaScript code, calculate the constant expression, obtain a calculation result, and convert the constant expression into a calculation result.

[0036] In some embodiments of the present application, the method further comprises:

[0037] Find the continuous if statements in the third optimized JavaScript code, and convert the continuous if statements into if-else if statements.

[0038] The present invention discloses a JavaScript optimization method based on precompilation technology, comprising: obtaining JavaScript code, searching and replacing the JavaScript code to obtain a first optimized JavaScript code; performing for loop optimization on the first optimized JavaScript code to obtain a second optimized JavaScript code; determining whether the second optimized JavaScript code contains multiple continuous variable declarations, and if so, converting the multiple continuous variable declarations into a single statement declaration; searching for arrays and objects created using a constructor in the second optimized JavaScript code, converting the arrays and objects into literals, and setting the converted second optimized JavaScript code as a third optimized JavaScript code; and performing on-site updating on the third optimized JavaScript code to complete the optimization of JavaScript.

[0039] This patent uses a method that performs a series of pre-compilation processes on JavaScript code before execution, which can more thoroughly optimize code performance and efficiency, and is less dependent on the developer's coding habits and technical level, and is less affected by the developer's subjective factors, thereby improving the stability of the code.

[0040] The technical solution of the present invention is further described in detail below through the accompanying drawings and embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] FIG1 is a flow chart of a JavaScript optimization method based on precompilation technology according to the present invention. DETAILED DESCRIPTION

[0042] The technical solution of the present invention is further described below with reference to the accompanying drawings and embodiments.

[0043] It should be noted that the following detailed descriptions are illustrative and intended to provide further explanation of the present invention. Unless otherwise specified, all technical and scientific terms used herein have the same meanings as those commonly understood by those skilled in the art to which the present invention belongs.

[0044] It should be noted that the terms used herein are intended only to describe specific embodiments and are not intended to limit the exemplary embodiments of the present invention. As used herein, unless the context clearly indicates otherwise, the singular is intended to include the plural. Furthermore, it should be understood that when the terms "comprising" and / or "including" are used in this specification, they specify the presence of features, steps, operations, devices, components, and / or combinations thereof, and do not exclude other elements or objects. The terms "first," "second," and similar terms used in the present invention do not denote any order, quantity, or importance, but are simply used to distinguish between different components. Terms such as "upper," "lower," "left," "right," "front," "rear," "vertical," "horizontal," "side," and "bottom" indicate orientations or positional relationships based on the orientations or positional relationships shown in the accompanying drawings. These terms are merely relative terms defined to facilitate the description of the structural relationships between the various components or elements of the present invention and do not specifically refer to any particular component or element in the invention and should not be construed as limiting the invention. Terms such as "fixed," "connected," and "connected" should be interpreted broadly to mean that a connection can be fixed, integral, or detachable; it can be direct or indirect through an intermediary. Relevant scientific research or technical personnel in this field can determine the specific meanings of the above terms in the present invention based on specific circumstances, and they should not be understood as limitations on the present invention.

[0045] Example

[0046] The present invention provides a JavaScript optimization method based on precompilation technology, as shown in FIG1 , comprising:

[0047] S1, obtain JavaScript code, search and replace the JavaScript code, and obtain a first optimized JavaScript code.

[0048] S2: Perform for loop optimization on the first optimized JavaScript code to obtain a second optimized JavaScript code.

[0049] S3: Determine whether the second optimized JavaScript code contains multiple consecutive variable declarations. If so, convert the multiple consecutive variable declarations into a single statement declaration.

[0050] S4: Find the arrays and objects created by using the constructor in the second optimized JavaScript code, convert the arrays and objects into literals, and set the converted second optimized JavaScript code as the third optimized JavaScript code.

[0051] S5, performing on-site update on the third optimized JavaScript code to complete the optimization of JavaScript.

[0052] In some embodiments of the present application, searching and replacing the JavaScript code includes:

[0053] Search for global variables in the JavaScript code and replace the global variables.

[0054] Find the with statement in the JavaScript code and replace the with statement.

[0055] Find the object attributes in the JavaScript code and replace the object attributes.

[0056] In some embodiments of the present application, searching for global variables in the JavaScript code and replacing the global variables include:

[0057] Searching for a global variable in the JavaScript code, and if multiple references to the same global variable exist within the same scope, saving the global variable as a first local variable, and replacing references to the global variable with references to the first local variable.

[0058] In this embodiment, repeated references to global variables are searched. If there are multiple references to the same global variable in the same scope, the first reference is saved as a local variable, and subsequent references to the global variable are replaced with references to the local variable.

[0059] For example, the following code:

[0060] The above code contains three references to the global variable document, which can be optimized into the following code:

[0061] In some embodiments of the present application, searching for a with statement in the JavaScript code and replacing the with statement includes:

[0062] Find a with statement in the JavaScript code, save the variable contained in the with statement as a second local variable, and replace the with statement with the second local variable.

[0063] In this embodiment, the with statement is replaced.

[0064] For example, the following function

[0065] Using local variables can achieve the same effect and reduce the need for additional scope chain lookups, such as:

[0066] In some embodiments of the present application, searching for object properties in the JavaScript code and replacing the object properties include:

[0067] Searching for an object property in the JavaScript code, and if multiple references to the same object property exist within the same scope, saving the object property as a third local variable, and replacing the references to the object property with references to the third local variable.

[0068] In this embodiment, for example, the following statements:

[0069] var query=window.location.href.substring(window.location.href.indexOf('?'));

[0070] This statement has 6 attribute lookups, 3 each for window.location.href.substring() and window.location.href.indexOf(). The same object attribute is used multiple times, which is saved in a third local variable. Modify it to:

[0071] var url=window.location.href;

[0072] var query=url.substring(url.indexOf('?'));

[0073] 6 attribute lookups were optimized to 4.

[0074] In some embodiments of the present application, before searching for the object properties in the JavaScript code, the method further includes:

[0075] Determine whether an array-like object exists in the JavaScript code; if so, replace property lookup of the array-like object with value lookup.

[0076] In this embodiment, such as the following statement:

[0077] var imgs=document.querySelectorAll('.img')

[0078] console.log(imgs.item(0))

[0079] var imgs2=document.getElementsByTagName('div')

[0080] console.log(imgs2.item(0))

[0081] In the above statement, document.querySelectorAll returns a NodeList object, and document.getElementsByTagName returns an HTMLCollection object. They are both array-like objects, and the item() method on them belongs to attribute search. Changing to value search is more efficient. Modify it to

[0082] var imgs=document.querySelectorAll('.img')

[0083] console.log(imgs[0])

[0084] var imgs2=document.getElementsByTagName('div')

[0085] console.log(imgs2[0])

[0086] In some embodiments of the present application, performing for loop optimization on the first optimized JavaScript code includes:

[0087] Finding a for loop in the first optimized JavaScript code, determining the amount of data in a data set of the for loop, the time required to process the data set of the for loop, and the algorithmic complexity of the for loop;

[0088] If the data volume of the data set of the for loop exceeds a preset data volume, or the time for processing the data set of the for loop is greater than a preset time, or the algorithm complexity of the for loop is greater than O(n), then the data set of the for loop is determined to be a large data set, and the for loop is processed using a Duff device;

[0089] Otherwise, use the multiple function to process the for loop;

[0090] If the termination condition of the for loop is accessing the target object attribute, the object attribute is replaced.

[0091] In this embodiment, the Duff device is used to process a large data set loop as follows:

[0092] The above process limits the number of elements processed in each loop to 8, reducing the number of loop iterations. Furthermore, by determining the number of remaining elements, the process determines whether to proceed to the remaining elements processing logic (not looping), further reducing the number of loop iterations.

[0093] Look at the following situation

[0094] Since the number of loops is fixed and small, we can eliminate the loop by changing it to multiple function calls, see below.

[0095] For example, the following situation

[0096] The termination condition in this loop needs to access the properties of the values ​​object and replace them with local variables, see below.

[0097] In some embodiments of the present application, performing a localized update on the third optimized JavaScript code includes:

[0098] Find the appendChild call in the third optimized JavaScript code, and use document fragments to build a DOM structure to perform on-site update on the appendChild call.

[0099] The specific implementation example of on-site update is, for example, the following code

[0100] This code adds 10 items to the list. As each item is added, there are two field updates: one to add element, and one to add a text node to it. Thus, this operation requires a total of 20 live updates. The following optimizations can be performed

[0101] There is only one live update after the optimization is complete, which happens after all projects are created.

[0102] In some embodiments of the present application, the method further comprises:

[0103] Determine whether the string of the JavaScript code is contained in eval, setTimeout, setInterval, or new Function. If so, embed the code corresponding to the string in the code of eval, setTimeout, or setInterval. If so, convert the new Function into a general function.

[0104] In this embodiment, if the string is included in eval, setTimeout, or setInterval, the code corresponding to the string is embedded in the code of eval, setTimeout, or setInterval. If the string is included in new Function, the new Function is converted to a general function.

[0105] For example, the following code

[0106] eval("alert('hello!')");

[0107] setTimeout("alert('hello!')",300);

[0108] setInterval("alert('hello!')",300);

[0109] var sayHi=new Function("alert('hello!')");

[0110] The above codes all need to parse the string containing Javascript code. When running the Javascript code, a new interpreter needs to be started to parse the new code, which generates unnecessary extra overhead. Convert it to a general function, see below

[0111] In some embodiments of the present application, the method further comprises:

[0112] Search for a constant expression in the third optimized JavaScript code, calculate the constant expression, obtain a calculation result, and convert the constant expression into a calculation result.

[0113] In this embodiment, the specific code example of constant expression optimization is:

[0114] var x=2+3;

[0115] Optimized to

[0116] var x=5;

[0117] Reduce the amount of calculation in the code and optimize execution speed.

[0118] In some embodiments of the present application, the method further comprises:

[0119] Find the continuous if statements in the third optimized JavaScript code, and convert the continuous if statements into if-else if statements.

[0120] In this embodiment, a specific code example for converting the continuous if statement into an if-else if statement is as follows:

[0121] The branch structure in the code is reduced, and the execution efficiency is improved.

[0122] In step S3, the specific code example for optimizing multiple consecutive variable declarations is:

[0123] var count = 5;

[0124] var color = 'blue';

[0125] var now = new Date();

[0126] Converting it to a single statement declaration is much faster than separate declarations, see below

[0127] var count=5, color='blue', now=new Date().

[0128] In step S4, the specific code examples for optimizing arrays and objects created using the constructor are as follows:

[0129] var values ​​= new Array();

[0130] values[0] = 123;

[0131] values[1]=456;

[0132] values[2]=789;

[0133] var person = new Object();

[0134] person.name = 'Jack';

[0135] person.age = 20;

[0136] person.sayName=function(){

[0137] alert(this.name);

[0138] }

[0139] The above code only creates and initializes an array and an object, using four statements for each: one to call the constructor and three to allocate data.

[0140] After rewriting, 8 statements became 2.

[0141] The present invention discloses a JavaScript optimization method based on precompilation technology, which can optimize code performance and efficiency more thoroughly than execution-time optimization, is less dependent on the coding habits and technical level of developers, and improves code stability.

[0142] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention rather than to limit the same. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that they can still modify or replace the technical solutions of the present invention with equivalents, and these modifications or equivalent replacements cannot cause the modified technical solutions to deviate from the spirit and scope of the technical solutions of the present invention.

[0143] The system provided in the above embodiment is only illustrated by the division of the above functional modules. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the modules or steps in the embodiments of the present invention can be further decomposed or combined. For example, the modules in the above embodiment can be combined into one module or further divided into multiple sub-modules to complete all or part of the functions described above. The names of the modules and steps involved in the embodiments of the present invention are only for the purpose of distinguishing the modules or steps and are not to be regarded as improper limitations of the present invention.

[0144] Those skilled in the art should be able to appreciate that, in conjunction with the modules and method steps of each example described in the embodiments disclosed herein, it is possible to implement them with electronic hardware, computer software, or a combination of the two, and the programs corresponding to the software modules and method steps can be placed in random access memory (RAM), internal memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disks, removable disks, CD-ROMs, or any other form of storage medium known in the art. In order to clearly illustrate the interchangeability of electronic hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in electronic hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the present invention.

Claims

1. A JavaScript optimization method based on precompilation technology, It is characterized in that include: Obtaining JavaScript code, searching and replacing the JavaScript code to obtain a first optimized JavaScript code; Perform for loop optimization on the first optimized JavaScript code to obtain a second optimized JavaScript code; Determine whether the second optimized JavaScript code contains multiple consecutive variable declarations, and if so, convert the multiple consecutive variable declarations into a single statement declaration; Find the array and object created by the constructor in the second optimized JavaScript code, convert the array and object into literals, and set the converted second optimized JavaScript code as the third optimized JavaScript code; The third optimized JavaScript code is updated on-site to complete the optimization of JavaScript.

2. According to claim 1, a JavaScript optimization method based on precompilation technology, It is characterized in that Search and replace the JavaScript code, including: Searching for global variables in the JavaScript code and replacing the global variables; Find the with statement in the JavaScript code and replace the with statement; Find the object properties in the JavaScript code and replace the object properties.

3. According to claim 2, a JavaScript optimization method based on precompilation technology, It is characterized in that Finding global variables in the JavaScript code and replacing the global variables includes: Searching for global variables in the JavaScript code, if there are multiple references to the same global variable in the same scope, saving the global variable as a first local variable, and replacing the references to the global variable with references to the first local variable.

4. The JavaScript optimization method based on precompilation technology according to claim 2, It is characterized in that Find the with statement in the JavaScript code and replace the with statement, including: Find a with statement in the JavaScript code, save the variable included in the with statement as a second local variable, and replace the with statement with the second local variable.

5. The JavaScript optimization method based on precompilation technology according to claim 2, It is characterized in that Finding object properties in the JavaScript code and replacing the object properties includes: Searching for an object property in the JavaScript code, if there are multiple references to the same object property in the same scope, saving the object property as a third local variable, and replacing the references to the object property with references to the third local variable.

6. The JavaScript optimization method based on precompilation technology according to claim 5, It is characterized in that Before looking up the object properties in the JavaScript code, also include: It is determined whether there is an array-like object in the JavaScript code. If there is an array-like object, the property search of the array-like object is replaced by the value search.

7. The JavaScript optimization method based on precompilation technology according to claim 1, It is characterized in that The for loop optimization is performed on the first optimized JavaScript code, including: Finding a for loop in the first optimized JavaScript code, determining the amount of data in the data set of the for loop, the time for processing the data set of the for loop, and the algorithm complexity of the for loop; If the data volume of the data set of the for loop exceeds the preset data volume, or the time for processing the data set of the for loop is greater than the preset time, or the algorithm complexity of the for loop is greater than O(n), then the data set of the for loop is determined to be a large data set, and the for loop is processed by using a Duff device; Otherwise, use the multiple function to process the for loop; If the termination condition of the for loop is accessing the target object attribute, the object attribute is replaced.

8. The JavaScript optimization method based on precompilation technology according to claim 1, It is characterized in that The third optimized JavaScript code is updated on-site, including: Find the appendChild call in the third optimized JavaScript code, taking advantage of document fragmentation Build the DOM structure to perform in-place updates on the appendChild call.

9. The JavaScript optimization method based on precompilation technology according to claim 1, It is characterized in that The method further comprises: Determine whether the string of the JavaScript code is contained in eval, setTimeout, setInterval, or new Function. If so, embed the code corresponding to the string in the code of eval, setTimeout, or setInterval. If so, convert the new Function into a general function.

10. The JavaScript optimization method based on precompilation technology according to claim 1, It is characterized in that The method further comprises: Searching for a constant expression in the third optimized JavaScript code, calculating the constant expression, obtaining a calculation result, and converting the constant expression into a calculation result; Find the continuous if statements in the third optimized JavaScript code, and convert the continuous if statements into if-else if statements.

Citation Information

Patent Citations

  • Code file optimizing method, system and server

    CN104063208A

  • Multiversioning if statement merging and loop fusion

    US20090328021A1

  • Method of Adding Local Variables In Place of Global in JavaScript

    US20170161035A1

  • Method for optimizing Java performance using precompiled code

    US6289506B1