1

I recently had a quick look at the new jQuery Sizzle, i was wondering if there is any significant performance difference between using Sizzle.matchesSelector or assessing directly the attribute of the element parameter :

$.expr.createPseudo(function(selector) {
  return function( elem ) {
    return elem.getAttribute('data-smth').match(/someRegex/)
  }
}

VS :

$.expr.createPseudo(function(selector) {
  return function( elem ) {
    return $.find.matchesSelector(elem, 'div.someClass[data-smth*=smth]')
  }
}

I find personaly matchesSelector easier since we keep on jquery level and we can add easily some constraints (.someClass in my exemple above)

4

1 回答 1

1

不同之处在于它.matchesSelector()是一个原生函数或由 Sizzle 开发人员创建的自定义函数。在本机函数的情况下,差异虽然.getAttribute()仍然会更快,但会更小。但是,在自定义函数的情况下,差异会更大,绝对有利于.getAttribute().

.matchesSelector()但是,安全地查询属性,所以要记住这一点。

于 2012-11-19T23:03:16.570 回答