createPseudo performance



我最近快速查看了新的jQuery Sizzle,我想知道使用Sizzle.Matchesselector或直接评估元素参数的属性:

$.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]')
  }
}

我发现个性的Matchesselector更容易,因为我们保持在jQuery级别,并且可以轻松添加一些约束(.someclass(在我上面的典范中))

区别在于 .matchesSelector()是一个由sizzle开发人员创建的本机或自定义的函数。在本机函数的情况下,差异虽然.getAttribute()仍然会更快,但差异较小。但是,如果具有自定义功能,则差异会更大,绝对支持.getAttribute()

.matchesSelector()但是,安全查询属性,因此要牢记。

最新更新