jQuery正则表达式补丁导致CPU消耗高 [英] High cpu consumption due to a jquery regex patch

查看:117
本文介绍了jQuery正则表达式补丁导致CPU消耗高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将从事其他人开发的项目.我没有和这个人有任何联系. 网页加载后,我发现浏览器的CPU消耗很高.

I am going to work on a project developped by someone else. I don't have any contact with this guy. I noticed a very high cpu consumption by the browser when the page is loaded.

经过一些调查,我认为问题来自于jQuery正则表达式的补丁,但我不清楚该补丁的目标以及导致CPU消耗的原因.

After some investigations, i think that the problem comes from a patch of jquery regex but I don't clearly understand the goal of this patch and the cause of this cpu consumption.

jQuery.expr[':'].regex = function(elem, index, match) {
   var matchParams = match[3].split(',');
   var validLabels = /^(data|css):/;
   var attr = {
      method:
         matchParams[0].match(validLabels) ? matchParams[0].split(':')[0] : 'attr',
         property: matchParams.shift().replace(validLabels,'')
   };
   var regexFlags = 'ig';
   var regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
   return regex.test(jQuery(elem)[attr.method](attr.property));
};

我已经看到这种方法被恒定地调用.

I have seen that this method was constanly called.

我有几个这样的正则表达式调用.

I have several calls to regex like this one.

$(':regex(id,DelCompo.*$)').livequery('click',function(e) {
   //...
}

如果我对它们进行评论,则表明CPU消耗似乎是正常的.当然,某些功能已损坏.

If I comment them, the cpu consumption seems to be normal. Of course, some features are broken.

我认为问题出在这个新的正则表达式功能上.这是正确的吗? 解决此问题的最佳方法是什么?

I think the problem comes from this new regex function. Is it correct? What would be the best way to fix this problem?

谢谢

推荐答案

仅供参考,我通过不在jQuery选择器中使用正则表达式来解决此问题.我想这不是我的文档的正确选择.

FYI, I fixed it by not using regex in the jquery selector. I guess that this was not the right choice for my document.

这篇关于jQuery正则表达式补丁导致CPU消耗高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆