当前位置: 移动技术网 > IT编程>开发语言>JavaScript > javascript日期处理函数,性能优化批处理

javascript日期处理函数,性能优化批处理

2019年08月01日  | 移动技术网IT编程  | 我要评论

其实网上写javascript日期格式化的博文很多,大体都看了看,都还不错。唯一遗憾的是只顾着实现了功能,没对函数进行性能优化。
俗话说:不要重复造轮子。google上找了一个比较不错的日期格式化函数,来开始我的优化之旅吧!
google上找的这个日期函数化函数,估计大家都很眼熟,以前我也一直在用。先看看优化后和优化前的效率对比吧!
1、优化之前的todate函数(字符串转换成date对象),重复执行1万次,耗时660毫秒

javascript日期处置函数,对批处理做了性能优化

2、优化之前的dateformat函数(date对象格式化成字符串),重复执行1万次,耗时676毫秒

javascript日期处置函数,对批处理做了性能优化

3、优化过后的todate函数,重复执行1万次,耗时122毫秒

javascript日期处置函数,对批处理做了性能优化

4、优化后的dateformat函数,重复执行1万次,耗时160毫秒

javascript日期处置函数,对批处理做了性能优化

为什么前后差别这么大,其实我也没做多少处理,只是为批处理做了一些缓存而已,认真观察所有网上那些日期格式函数,其实都是用正则进行匹配和替换。其实正则是很耗性能的,于是我在正则匹配的地方做了缓存,把匹配值建立索引。以后就不用每次都去做正则匹配了。

无代码无真相,接下来看看真相吧!

(function(window) {
  var sinojh = {
    version : "1.2",
    copyright : "copyright© sino-jh 2012",
    author : "jeff lan",
    email : "jefflan@live.cn"
  };
  /**
   * 方便于添加和重写类的属性
   * @param {object} attributes 添加的属性
   */
  function.prototype.prototypes = function(attributes) {
    for ( var a in attributes) {
      this.prototype[a] = attributes[a];
    }
  };
  /**
   * 获取url参数
   * @param {string} parameter 参数名
   * @return {string} 参数值
   */
  sinojh.geturlparameter = function(parameter) {
    if (!sinojh.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);
      }
      sinojh.geturlparameter.cache = cache;
    }
    return sinojh.geturlparameter.cache[parameter];
  };
  /**
   * 日期格式化
   * @param {date} date 日期对象
   * @param {string} formatstyle 格式化样式
   * @return {string} 日期型字符串
   */
  sinojh.dateformat = function(date, formatstyle) {
    formatstyle = formatstyle ? formatstyle : sinojh.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 == sinojh.dateformat.formatstylecache) {
      var replacecache = sinojh.dateformat.replacecache;
      if (replacecache["y+"]) {
        formatstyle = formatstyle.replace(replacecache["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 {
      sinojh.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));
        }
      }
      sinojh.dateformat.replacecache = replacecache;
    }
    return formatstyle;
  };
  sinojh.dateformat.settings = {
    formatstyle : "yyyy-mm-dd hh:mm:ss"
  };
  /**
   * 将日期格式的字符串转换成date对象
   * @param {string} datestr 日期格式字符串
   * @param {string} datestyle 日期格式
   * @return {date} 日期对象
   */
  sinojh.todate = function(datestr, datestyle) {
    datestyle = datestyle ? datestyle : sinojh.todate.settings.datestyle;
    var compare = sinojh.todate.compare;
    var result = new sinojh.todate.result();
    if (datestyle == sinojh.todate.settings.datestylecache) {
      var indexcache = sinojh.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);
        }
      }
      sinojh.todate.indexcache = indexcache;
      sinojh.todate.settings.datestylecache = datestyle;
    }
    return new date(result["y"], result["m"] - 1, result["d"], result["h"], result["m"], result["s"], result["s"]);
  };
  sinojh.todate.compare = {
    "y+" : "y",
    "m+" : "m",
    "d+" : "d",
    "h+" : "h",
    "m+" : "m",
    "s+" : "s",
    "s" : "s"
  };
  sinojh.todate.result = function() {
  };
  sinojh.todate.result.prototypes( {
    "y" : "",
    "m" : "",
    "d" : "",
    "h" : "00",
    "m" : "00",
    "s" : "00",
    "s" : "000"
  });
  sinojh.todate.settings = {
    datestyle : "yyyy-mm-dd hh:mm:ss"
  };
  delete function.prototype.prototypes;
  window.jh = sinojh;
}(this); 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网