JavaScript functions: Optimizations for generating fixed result functions in JavaScript

Source: Internet
Author: User
Tags date functions log return

Share a JavaScript optimization technique for writing functions.
The applicable function should satisfy the following criteria:
produce a fixed result
Multiple calls on the page
Complex or more time-consuming
Code and analysis are as follows:
A function that produces a fixed result and is called more than once in the page
function Check () {
Simulate time-consuming operations
var begin = Date.now (); ECMAScript5 added, if not supported please change to +new Date ();
var one_second = 1000,
result = false;
while (true) {
if (Date.now ()-Begin >= One_second) {
result = true;
Break
}
}
function overrides, returning directly to the result
Check = function () {
return result;
}
return result;
}
var firstbegin = Date.now ();
Check (); First function call
var firstend = Date.now ();
Check (); Second function call
var secondend = Date.now ();
Console.log ("First function time consuming:" + (Firstend-firstbegin) + "Ms.");
Console.log ("Second function time consuming:" + (Secondend-firstend) + "Ms.");
The results appear as follows:
This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130109/35750.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.