Javascript date processing functions, performance optimization batch processing, and javascript Performance Optimization

Source: Internet
Author: User

Javascript date processing functions, performance optimization batch processing, and javascript Performance Optimization

In fact, there are a lot of blog posts about writing javascript date formatting on the Internet, which are generally looked at and are not bad. The only pity is that the function is only implemented and the performance of the function is not optimized.
As the saying goes: do not duplicate the wheel. Google has found a good date Formatting Function to start my optimization journey!
I have been using this date function on google. Let's take a look at the efficiency comparison after optimization and before optimization!
1. Optimize the previous toDate function (string converted to Date object), Repeat 10 thousand times, taking 660 milliseconds

2. Optimize the previous dateFormat function (format the Date object as a string), Repeat 10 thousand times, And take 676 milliseconds

3. The optimized toDate function repeats 10 thousand times and takes 122 milliseconds.

4. The optimized dateFormat function repeats 10 thousand times and takes 160 milliseconds.

Why is the difference so big? In fact, I didn't do much processing, But I just made some cache for batch processing. I carefully observed all the online date format functions, in fact, regular expressions are used for matching and replacement. In fact, regular expressions consume a lot of performance, so I cache the regular expression matching and index the matching value. In the future, you do not need to perform regular matching every time.

No code, no truth. Let's take a look at the truth!

(Function (window) {var sinoiterator = {Version: "1.2", Copyright: "Copyright©Sino-example 2012 ", Author:" Jeff Lan ", Email:" jefflan@live.cn "}; /*** it is convenient to add and override attributes of the class * @ param {Object} attributes added attributes */Function. prototype. prototypes = function (attributes) {for (var a in attributes) {this. prototype [a] = attributes [a] ;}}; /*** obtain the Url parameter * @ param {String} parameter name * @ return {String} parameter value */sinoworkflow. getUrlParameter = function (parameter) {if (! Sinoloud. getUrlParameter. cache) {var url = window. location. href; var paraString = url. substring (url. indexOf ("? ") + 1, url. length ). split ("&"); var cache ={}; for (var I in paraString) {var j = paraString [I]; cache [j. substring (0, j. indexOf ("=")] = j. substring (j. indexOf ("=") + 1, j. length);} sinoworkflow. getUrlParameter. cache = cache;} return sinoworkflow. getUrlParameter. cache [parameter] ;}; /*** format the Date * @ param {date} Date date object * @ param {String} formatStyle * @ return {String} Date String */sinoworkflow. dateFormat = fu Nction (date, formatStyle) {formatStyle = formatStyle? FormatStyle: sinoworkflow. dateFormat. settings. formatStyle; var time = {"M +": date. getMonth () + 1, "d +": date. getDate (), "h +": date. getHours (), "m +": date. getMinutes (), "s +": date. getSeconds (), "S": date. getMilliseconds ()}; if (formatStyle = sinoworkflow. dateFormat. formatStyleCache) {var replaceCache = sinostrap. dateFormat. replaceCache; if (replaceCache ["y +"]) {formatStyle = formatStyle. replace (replac ECache ["y +"]. replace, (date. getFullYear () + ""). substring (replaceCache ["y +"]. index);} for (var k in time) {if (replaceCache [k]) {formatStyle = formatStyle. replace (replaceCache [k]. replace, replaceCache [k]. replace. length = 1? Time [k]: ("00" + time [k]). substring ("" + time [k]). length) ;}} else {sinoworkflow. dateFormat. formatStyleCache = formatStyle; var replaceCache = {}; if (new RegExp ("(y + )"). test (formatStyle) {var index = 4-RegExp. $1. length; replaceCache ["y +"] = {replace: RegExp. $1, index: index}; formatStyle = formatStyle. replace (RegExp. $1, (date. getFullYear () + ""). substring (index);} for (var k in time) {If (new RegExp ("(" + k + ")"). test (formatStyle) {replaceCache [k] = {replace: RegExp. $1}; formatStyle = formatStyle. replace (RegExp. $1, RegExp. $1. length = 1? Time [k]: ("00" + time [k]). substring ("" + time [k]). length) ;}} sino=. dateFormat. replaceCache = replaceCache;} return formatStyle;}; sinoworkflow. dateFormat. settings = {formatStyle: "yyyy-MM-dd hh: mm: ss "}; /*** convert a Date String to a Date object * @ param {String} dateStr Date Format String * @ param {String} dateStyle Date format * @ return {Date} Date object */sinoworkflow. toDate = function (dateStr, dateStyle) {dateStyle = dateStyle? DateStyle: sinoworkflow. toDate. settings. dateStyle; var compare = sinoworkflow. toDate. compare; var result = new sinoworkflow. toDate. result (); if (dateStyle = sinoworkflow. toDate. settings. dateStyleCache) {var indexCache = sinostrap. toDate. indexCache; for (var k in compare) {if (indexCache [k]) {result [compare [k] = dateStr. substring (indexCache [k]. index, indexCache [k]. index + indexCache [k]. length) ;}} else {var indexCache ={}; for (var k in compare) {if (new RegExp ("(" + k + ")"). test (dateStyle) {var index = dateStyle. indexOf (RegExp. $1); var length = RegExp. $1. length; indexCache [k] = {index: index, length: length}; result [compare [k] = dateStr. substring (index, index + length) ;}} sino=. toDate. indexCache = indexCache; sinostrap. toDate. settings. dateStyleCache = dateStyle;} return new Date (result ["y"], result ["M"]-1, result ["d"], result ["h"], result ["m"], result ["s"], result ["S"]) ;}; sino=. toDate. compare = {"y +": "y", "M +": "M", "d +": "d", "h +": "h ", "m +": "m", "s +": "s", "S": "S"}; sinoworkflow. toDate. result = function () {}; sino=. toDate. result. prototypes ({"y": "", "M": "", "d": "", "h": "00", "m": "00 ", "s": "00", "S": "000"}); sinoworkflow. toDate. settings = {dateStyle: "yyyy-MM-dd hh: mm: ss"}; delete Function. prototype. prototypes; window. vertex = sinoiterator;} (this );

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.