Note: Below is a translation of a note from the Julien Lecomte YUI utilities blog “Running CPU Intensive JavaScript Computations in a Web Browser” , in which the author considers how to perform “heavy” calculations in a web browser and present a number of methods for their “optimization”.My comments are in italics.
Introduction
The template that I want to discuss below is well known and used for over 10 years. The purpose of this note is to present this template in a new light and, more importantly, to discuss possible ways to reduce overhead costs. ')
The most significant obstacle to performing “heavy” calculations in a web browser is the fact that the entire user interface in the browser stops and waits for the end of the execution of the JavaScript code. This means that under no circumstances should we allow the completion of the script to take more than 300 ms (or better, if it is much less). Violation of this rule inevitably leads to poor perception of the resource by the user ( bad user experience ).
In addition, in web browsers, the JavaScript process has a limited time to complete its execution (this can be a fixed number - in the case of browsers on the Mozilla engine - or some other restriction, for example, the maximum number of elementary operations - in the case of Internet Explorer). If the script runs too long, the user is presented with a dialog box in which he is asked if the script should be interrupted.