SoFunction
Updated on 2025-04-12

Detailed introduction to functions in JavaScript

If you need to know the time of code execution during web debugging, you can time the execution of the program by adding () statements and () statements to the JavaScript code. The following long-term foo() function is an example:


Copy the codeThe code is as follows:

function foo(){
    var x = 4.237;
    var y = 0;
    for (var i=0; i<100000000; i++) {
        y = y + x*x;
    }
    return y;
}


If you need to know how long it takes during the execution of the function, you can insert the() statement before the foo() function call and the() statement after the call is finished:


Copy the codeThe code is as follows:

("test");
foo();
("test");


After the program is executed, the console will display the result of this time: "test: 1797ms", and the log level displayed is info.

() and () accept a string as an argument, which is equivalent to the timed id. The browser will pair () with the same parameter (id) with () to record the time difference between the two. Therefore, different places in JavaScript programs can be timed by using different ids.

Browser support

For each browser, the support for () timing is as follows:

Firefox. Native support after 10.0. For previous versions of Firefox, it can be achieved by installing the Firebug plugin. See:/en-US/docs/Web/API/?redirectlocale=en-US&redirectslug=DOM%
Google Chrome. Native support after 2.0. See:/chrome-developer-tools/docs/console-api#consoletimelabel
IE. Native support in IE11. For previous versions of IE, it can be achieved by installing Firebug Lite. See:/en-us/library/ie/dn265071%28v=vs.85%
Safari. Native support after 4.0. See:/library/safari/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/Console/
Opera. support. See:/dragonfly/documentation/console/