jQuery选择器的性能测试
来源:互联网 发布:索尼手机录音软件 编辑:程序博客网 时间:2024/06/15 04:25
在https://github.com/doomhz/jQuery-Tweaks 上面看到jQuery性能测试工具:
/*** Doom Tweaks for Javascript Projects** @author Dumitru Glavan* @version 1.1 (16-JUL-2011)* @requires jQuery* @link http://dumitruglavan.com** @example: $.l(4,5,6);* @example: $.time();* @example: $.lt();$('div')$.lt();* @example: $.bm('$('div')'); - benchmark your code* @example: $.mockAjax({mockUrl: '/ajax_mocks'}); - mock your ajax calls** Dual licensed under the MIT and GPL licenses:* http://www.opensource.org/licenses/mit-license.php* http://www.gnu.org/licenses/gpl.html**/(function ($) { /** * Extend Firebug */if (typeof(console) === 'object') {/** * Shortcut function for console.log() */$.extend($, { l: function () { for (var i = 0; i < arguments.length; i++) { console.log(arguments[i]); } } });}/** * Shortcut function for getting timestamp in second (PHP like function time()) * @param numeric divideBy - You can switch back to milliseconds by specifying this as 1 */$.extend($, { time: function (divideBy) {return ~~(+ (new Date().getTime() / (typeof divideBy === 'undefined' ? 1000 : divideBy)));} });/** * Shortcut function for logging time to the Firebug console * call $.lt() then your code then $.lt() again to get the results */$.extend($, { lt: function () { if (this.ltLastTime == null) { return this.ltLastTime = new Date().getTime(); } var diff = new Date().getTime() - this.ltLastTime; this.ltLastTime = null; $.l(diff); return diff; }, ltLastTime: null});/** * Shortcut function for benchmarking a block of code to the Firebug console * this function will run your code in a for block to create overflow and push the results into Firebug * * @param string benchmarkCode - the block of code you want to benchmark * @param numeric testTime - the number of FOR cicles */$.extend($, { bm: function (benchmarkCode, testTime) { this.testTime = typeof testTime === 'number' ? testTime : 9999; $.lt(); for (var i = 0;i < this.testTime;i++) { eval(benchmarkCode); } $.lt(); }}); /** * Mock ajax requests with a prefilter * */ $.extend($, { mockAjax: function (mockOptions) { mockOptions = $.extend({ mockUrl: '/ajax_mocks' }, mockOptions); $.ajaxPrefilter(function(options, originalOptions, jqXHR) { if (!options.noMock) { options.url = mockOptions.mockUrl + '?ajax_url=' + encodeURIComponent(options.url); } }); }});})(jQuery);
$("id");
$("class");
$("id class");
$("class","id");
$("id").find("class");
发现并没有太大的差别;然后查看了一下代码,原来是因为工具函数里面用了eval()方法。
eval()到底有多耗性能?下面做个简单的测试(调用9998次后所需的时间(ms)):
通过几次测试之后,eval()这个方法的所耗时间竟然是$("id")的18--19倍。
这也是为什么eval()方法需要谨慎使用的原因。
如果直接用上面的工具函数来测试jQury方法的性能,实在有点不够直观,
所以可以改用下面的方法来测试:
$.lt();for(i=1; i<9999; i++){$('#test1');}$.lt();当然在使用上没有封装好的方法那么方便,但因为不需要调用eval()方法,所以测出的数值更为直观。
下面再做几个测试,js部分代码如下:
$(document).ready(function () {$.extend($, {lt: function () {if (this.ltLastTime == null) {return this.ltLastTime = new Date().getTime();}var diff = new Date().getTime() - this.ltLastTime;this.ltLastTime = null;return diff;},ltLastTime: null});$.lt();for(i=1; i<99999; i++){document.getElementById("test");}console.log("getElementById('test'):", $.lt());//$("id");$.lt();for(i=1; i<99999; i++){$('#test');}console.log("$('#test'):", $.lt());//$("class");$.lt();for(i=1; i<99999; i++){$(".testdiv");}console.log("$('.testdiv'):", $.lt());//$("id class");$.lt();for(i=1; i<99999; i++){$("#test .testdiv");}console.log("$('#test .testdiv'):", $.lt());//$("class","id");$.lt();for(i=1; i<99999; i++){$(".testdiv", "#test");}console.log("$('.testdiv', '#test'):", $.lt());//$("id").find("class");$.lt();for(i=1; i<99999; i++){$("#test").find(".testdiv");}console.log("$('#test').find('.testdiv'):", $.lt());});
html的代码如下:
<div></div><div></div><div></div> <div></div><div></div><div></div> <div></div><div></div><div></div> <div></div><div></div><div></div> <div></div><div></div><div></div> <div id="test"> <div class="testdiv"></div> <div></div><div></div><div></div> <div></div><div></div><div></div> <div></div><div></div><div></div> <div></div><div></div><div></div> <div></div><div></div><div></div> <div></div><div></div><div></div> <div></div><div></div><div></div> <div class="testdiv"></div> <div class="testdiv"></div> <div class="testdiv"></div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div class="testdiv"></div><div class="testdiv"></div><div class="testdiv"></div>
Chrome的测试结果:
FireFox的测试结果:
可以看到,jQuery的选择器在性能和js还是有不少的差距。
而jQuery以#id选择器的速度比.class的速度要快很多,
如果针对特定的class进行选择时,
$("id").find("class");的效率是最高的!
而$("id class") 和 $("class","id")两个方法在两个浏览器之间不同的情况。
Deom
- jQuery选择器的性能测试
- jQuery选择器性能测试
- jQuery的选择器性能
- 101 jquery选择器性能分析
- 利用原生js提升jquery选择器的性能
- jQuery 的选择器 元素选择器
- Jquery选择器的基本选择器
- Jquery选择器的层次选择器
- Jquery-选择器-简单的选择器
- jQuery的常用选择器
- jQuery 选择器的使用
- jquery选择器的使用
- jquery的常用选择器
- jQuery 选择器的使用
- jQuery的选择器
- jQuery 选择器的使用
- jquery选择器的使用方法
- jQuery 选择器的使用
- 学习汇编的终极圣地
- Android编译重要参数LOCAL_MODULE_TAGS
- DBA_FREE_SPACE的file_id和relative_fno问题
- MessageBox in wpf
- Spring 3 MVC深入研究
- jQuery选择器的性能测试
- SDRAM内存驱动 的学习
- GridView 单击选择行,双击打开详细页面,鼠标移到某行上变色
- 关于ExtJS错误“例外被抛出且未被接住”问题
- 一片关于poi 读取xls写入数据库的文档
- Struts2中DispatherAction的功能
- mahout推荐介绍-3
- EXTJS 不显示窗体 标题框的办法
- ABAP学习开发总结之二:ABAP编辑器了解