【问题标题】:createPseudo performance创建伪性能
【发布时间】:2012-11-20 07:03:16
【问题描述】:

我最近快速浏览了新的 jQuery Sizzle,我想知道使用 Sizzle.matchesSelector 或直接评估元素参数的属性之间是否有任何显着的性能差异:

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

对比:

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

我发现个性化匹配选择器更容易,因为我们保持在 jquery 级别,我们可以轻松添加一些约束(在我上面的示例中为 .someClass)

【问题讨论】:

    标签: jquery jquery-selectors sizzle


    【解决方案1】:

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

    但是,.matchesSelector() 可以安全地查询属性,因此请牢记这一点。

    【讨论】:

      猜你喜欢
      • 2013-08-27
      • 1970-01-01
      • 2020-03-20
      • 2015-04-30
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多