A method for improving the efficiency of browser compression files
Patent Information
- Application Number
- CN202210663295.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-13
- Publication Date
- 2024-10-25
- Estimated Expiration
- 2042-06-13
AI Technical Summary
[0004]1)受限于JavaScript单线程语言运行速度的限制,对CPU密集型运算场景表现不佳,压缩大文件效率不够快
[0018] Using the above solution, the present invention provides a method for improving the efficiency of browser compression files. It arranges a multi-threaded compression library as a sub-thread of the browser, and uses sub-threads to compress files with a file size >10M, thereby effectively improving the file compression efficiency. Compression efficiency saves time on compressing large batches of files, thereby effectively improving user experience.
Smart Images

Figure CN114969595B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of file transmission, and in particular to a method for improving the efficiency of browser file compression. Background Art
[0002] When a browser uploads a large number of files, it often needs to compress the files before uploading to improve the upload efficiency and save server bandwidth and resources. At present, the browser often uses an open source solution such as JSZip to compress files.
[0003] The JSZip compression scheme has the following defects:
[0004] 1) Due to the limitation of JavaScript single-threaded language running speed, it performs poorly in CPU-intensive computing scenarios and is not efficient enough in compressing large files.
[0005] 2) When compressing a large number of files, the browser often freezes and becomes unresponsive.
[0006] 3) The underlying layer uses the DEFLATE algorithm for compression and does not support other mainstream high-speed compression algorithms such as ZSTD and LZ4.
[0007] Therefore, the prior art has defects and needs to be improved. Summary of the invention
[0008] The technical problem to be solved by the present invention is to provide a method for improving the efficiency of browser compression files, which effectively improves the efficiency of browser compression files and improves user experience.
[0009] The technical solution of the present invention is as follows: a method for improving the efficiency of browser compression files is provided, comprising the following steps.
[0010] S1: Encapsulate the zstd algorithm into a multi-threaded compression library, and use a compiler to compile the multi-threaded compression library into a JavaScript language variant Webassembly module to obtain compiled zip.wasm and zip.js. In this step, the zstd algorithm is encapsulated into a multi-threaded compression library using C language. The compiler uses Emscripten; the main thread is the UI thread.
[0011] S2: The browser is initialized, and a child thread Worker is created through the main thread of the browser. The Worker is loaded through zip.js, and the zip.wasm module is instantiated. The instantiated object is recorded as a wasm module; the wasm module notifies the main thread that it is in a ready state. In this step, the wasm module notifies the main thread that it is in a ready state through the first postMessage function; the message format of the first postMessage function is: postMessage({type:'inited'}).
[0012] S3: When a user uploads a large number of files in the browser, the browser obtains a list of files; the browser creates a set K to collect the compressed files.
[0013] S4: The browser compresses the file; when the file size is ≤10MB, the main thread is used to compress it directly, and the JSZip library is called directly for compression, and the compressed file is put into the set K; when the file size is greater than 10MB, the data stream of the file is used as a parameter, and the main thread is used to send the file to the wasm module in the Worker. The wasm module calls the compressFile function to compress the file, and then the wasm module passes the compressed file to the main thread. After receiving the message, the main thread puts the compressed file into the set K.
[0014] In this step, the main thread sends the message to the wasm module in the Worker through the second postMessage function; the message format of the second postMessage function is: postMessage({type:'file', file:File}), where File represents the file data stream of the file. The wasm module passes the compressed file to the main thread through the third postMessage function; the message format of the third postMessage function is: postMessage({type:'compressed', file:compressedFile}), where compressedFile is the compressed file.
[0015] S5: The browser compresses all the files separately by repeating step S4 and puts the compressed files into set K.
[0016] S6: When the length of the set K is equal to the length of the file list, it means that all files are compressed. After all files are compressed, the main thread calls the close function of the Worker to close the Worker to save resources.
[0017] In step S3, it also includes: traversing the file list with JavaScript language and numbering the files. In step S5, the browser compresses all the files in sequence according to the numbers by repeating step S4 and puts the compressed files into set K.
[0018] By adopting the above scheme, the present invention provides a method for improving the efficiency of browser file compression, arranging a multi-threaded compression library as a sub-thread of the browser, and using the sub-thread to compress files with a file size greater than 10M, thereby being able to effectively improve the compression efficiency and save the time spent on compressing large batches of files, thereby effectively improving the user experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 is a flow chart of an embodiment of the present invention. DETAILED DESCRIPTION
[0020] The present invention is described in detail below in conjunction with the accompanying drawings and specific embodiments.
[0021] See also Figure 1 , this embodiment provides a method for improving the efficiency of browser compression files, including the following steps.
[0022] S1: Encapsulate the zstd algorithm into a multi-threaded compression library using C language, and compile the multi-threaded compression library into a JavaScript language variant Webassembly module using a compiler to obtain compiled zip.wasm and zip.js. In this step, the compiler uses Emscripten; the main thread is the UI thread.
[0023] S2: The browser is initialized. A child thread Worker is created through the main thread of the browser. The Worker is loaded through zip.js, and the zip.wasm module is instantiated. The instantiated object is recorded as a wasm module. The wasm module notifies the main thread that it is in the ready state through the first postMessage function. The message format of the first postMessage function is: postMessage({type:'inited'}).
[0024] S3: When a user uploads a large number of files in the browser, the browser obtains a file list; the browser creates a set K to collect the compressed files. Use JavaScript to traverse the file list and number the files.
[0025] S4: The browser compresses the file; when the file size is ≤10MB, the main thread is used to compress it directly, and the JSZip library is called directly for compression, and the compressed file is put into the set K; when the file size is greater than 10MB, the data stream of the file is used as a parameter, and the main thread is used to send the file to the wasm module in the Worker. The wasm module calls the compressFile function to compress the file, and then the wasm module passes the compressed file to the main thread. After receiving the message, the main thread puts the compressed file into the set K.
[0026] In this step, the main thread sends the message to the wasm module in the Worker through the second postMessage function; the message format of the second postMessage function is: postMessage({type:'file', file:File}), where File represents the file data stream of the file. The wasm module passes the compressed file to the main thread through the third postMessage function; the message format of the third postMessage function is: postMessage({type:'compressed', file:compressedFile}), where compressedFile is the compressed file.
[0027] S5: The browser compresses all the files in sequence according to their numbers by repeating step S4 and puts the compressed files into set K.
[0028] S6: When the length of the set K is equal to the length of the file list, it means that all files are compressed. After all files are compressed, the main thread calls the close function of the Worker to close the Worker to save resources.
[0029] JavaScript execution logic is single-threaded. The above method allows the browser to start multiple threads to compress larger files, greatly improving compression efficiency. After testing, when uploading a file with a total size of 10GB, it takes 109.5 seconds to compress using JSzip alone. If the above method is used for compression, the total time is 18.9 seconds, and the compression efficiency is improved by about 5.7 times.
[0030] In summary, the present invention provides a method for improving the efficiency of browser file compression, arranges a multi-threaded compression library as a sub-thread of the browser, and uses the sub-thread to compress files with a file size greater than 10M, thereby effectively improving the compression efficiency and saving the time spent on compressing large batches of files, thereby effectively improving the user experience.
[0031] The above are only preferred embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for improving the efficiency of browser compression files, characterized in that: The following steps are involved: S1: Encapsulate the zstd algorithm into a multi-threaded compression library, and use a compiler to compile the multi-threaded compression library into a JavaScript language variant Webassembly module to obtain the compiled zip.wasm and zip.js; S2: The browser is initialized. A child thread Worker is created through the main thread of the browser. The Worker is loaded through zip.js, and the zip.wasm module is instantiated. The instantiated object is recorded as a wasm module. The wasm module notifies the main thread that it is in a ready state. S3: When a user uploads a large number of files in the browser, the browser obtains a list of files; the browser creates a set K to collect the compressed files; S4: The browser compresses the file; When the file size is ≤10MB, the main thread is used to compress the file, and the JSZip library is directly called to compress the file, and the compressed file is put into the set K. When the file size is greater than 10MB, the data stream of the file is used as a parameter, and the main thread is used to send the file to the wasm module in the Worker. The wasm module calls the compressFile function to compress the file, and then the wasm module passes the compressed file to the main thread. After receiving the message, the main thread puts the compressed file into the set K. S5: The browser compresses all the files separately by repeating step S4 and puts the compressed files into set K; S6: When the length of the set K is equal to the length of the file list, it means that all files have been compressed.
2. A method for improving the efficiency of browser compression files according to claim 1, characterized in that: In step S6, after all files are compressed, the main thread calls the close function of the Worker to close the Worker.
3. A method for improving the efficiency of browser compression files according to claim 1, characterized in that: In step S1, the zstd algorithm is encapsulated as a multi-threaded compression library using C language.
4. A method for improving the efficiency of browser compression files according to claim 1, characterized in that: In step S1, the compiler adopts Emscripten; the main thread is the UI thread.
5. A method for improving the efficiency of browser compression files according to claim 1, characterized in that: In step S2, the wasm module notifies the main thread that it is in the ready state through the first postMessage function; the message format of the first postMessage function is: postMessage({type:'inited'}).
6. A method for improving the efficiency of browser compression files according to claim 1, characterized in that: In step S3, it also includes: traversing the file list with JavaScript language and numbering the files. In step S5, the browser compresses all the files in sequence according to the numbers by repeating step S4 and puts the compressed files into set K.
7. A method for improving the efficiency of browser compression files according to claim 1, characterized in that: In step S4, the main thread sends the message to the wasm module in the Worker through the second postMessage function; the message format of the second postMessage function is: postMessage({type:'file', file:File}), where File represents the file data stream of the file.
8. A method for improving the efficiency of browser compression files according to claim 1, characterized in that: In step S4, the wasm module passes the compressed file to the main thread through the third postMessage function; the message format of the third postMessage function is: postMessage({type:'compressed', file:compressedFile}), where compressedFile is the compressed file.
