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

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.