javascript日期处理函数,性能优化批处理_基础知识
其实网上写javascript日期格式化的博文很多,大体都看了看,都还不错。唯一遗憾的是只顾着实现了功能,没对函数进行性能优化。
俗话说:不要重复造轮子。google上找了一个比较不错的日期格式化函数,来开始我的优化之旅吧!
google上找的这个日期函数化函数,估计大家都很眼熟,以前我也一直在用。先看看优化后和优化前的效率对比吧!
1、优化之前的toDate函数(字符串转换成Date对象),重复执行1万次,耗时660毫秒
2、优化之前的dateFormat函数(Date对象格式化成字符串),重复执行1万次,耗时676毫秒
3、优化过后的toDate函数,重复执行1万次,耗时122毫秒
4、优化后的dateFormat函数,重复执行1万次,耗时160毫秒
为什么前后差别这么大,其实我也没做多少处理,只是为批处理做了一些缓存而已,认真观察所有网上那些日期格式函数,其实都是用正则进行匹配和替换。其实正则是很耗性能的,于是我在正则匹配的地方做了缓存,把匹配值建立索引。以后就不用每次都去做正则匹配了。
无代码无真相,接下来看看真相吧!
(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);

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

为了提高Go应用程序的性能,我们可以采取以下优化措施:缓存:使用缓存减少对底层存储的访问次数,提高性能。并发:使用goroutine和channel并行执行冗长的任务。内存管理:手动管理内存(使用unsafe包)以进一步优化性能。为了横向扩展应用程序,我们可以实施以下技术:水平扩展(横向扩展):在多个服务器或节点上部署应用程序实例。负载均衡:使用负载均衡器将请求分配到多个应用程序实例。数据分片:将大型数据集分布在多个数据库或存储节点上,提高查询性能和可扩展性。

C++性能优化涉及多种技术,包括:1.避免动态分配;2.使用编译器优化标志;3.选择优化数据结构;4.应用缓存;5.并行编程。优化实战案例展示了如何在整数数组中查找最长上升子序列时应用这些技术,将算法效率从O(n^2)提升至O(nlogn)。

通过建立数学模型、进行模拟和优化参数,C++可显着提高火箭发动机性能:建立火箭发动机的数学模型,描述其行为。模拟发动机性能,计算关键参数(如推力和比冲)。识别关键参数并使用优化算法(如遗传算法)搜索最佳值。根据优化后的参数重新计算发动机性能,提高其整体效率。

通过实施缓存机制、并行处理、数据库优化和减少内存消耗,可以提升Java框架的性能。缓存机制:减少数据库或API请求次数,提高性能。并行处理:利用多核CPU同时执行任务,提高吞吐量。数据库优化:优化查询、使用索引、配置连接池,提升数据库性能。减少内存消耗:使用轻量级框架、避免泄漏、使用分析工具,减少内存消耗。

Java中的轮廓分析用于确定应用程序执行中的时间和资源消耗。使用JavaVisualVM实施轮廓分析:连接到JVM开启轮廓分析,设置采样间隔运行应用程序停止轮廓分析分析结果显示执行时间的树形视图。优化性能的方法包括:识别热点减少方法调用优化算法

程序性能优化方法包括:算法优化:选择时间复杂度更低的算法,减少循环和条件语句。数据结构选择:根据数据访问模式选择合适的数据结构,如查找树和哈希表。内存优化:避免创建不必要对象,释放不再使用的内存,使用内存池技术。线程优化:识别可并行化任务,优化线程同步机制。数据库优化:创建索引加快数据检索,优化查询语句,使用缓存或NoSQL数据库提升性能。

快速诊断PHP性能问题的有效技术包括:使用Xdebug获取性能数据,然后分析Cachegrind输出。使用Blackfire查看请求跟踪,生成性能报告。检查数据库查询,识别低效查询。分析内存使用情况,查看内存分配和峰值使用。

Nginx性能调优可以通过调整worker进程数、连接池大小、启用Gzip压缩和HTTP/2协议、使用缓存和负载均衡来实现。1.调整worker进程数和连接池大小:worker_processesauto;events{worker_connections1024;}。2.启用Gzip压缩和HTTP/2协议:http{gzipon;server{listen443sslhttp2;}}。3.使用缓存优化:http{proxy_cache_path/path/to/cachelevels=1:2k
