简体   繁体   English

JavaScript:多个过滤器链与复杂条件

[英]JavaScript: multiple filters chain vs. complex condition

Very often you need to filter an array with more than one condition:很多时候,您需要过滤具有多个条件的数组:

return results
  .filter(x => controlGroup[x] !== undefined)
  .filter(x => x.size > 10)

or you could do the same with a complex condition and a single filter :或者您可以对复杂条件和单个filter执行相同操作:

return results
  .filter(x => controlGroup[x] !== undefined && x.size > 10)
// or
  .filter(x => 
    controlGroup[x] !== undefined // <- Prettier would place && here. What a joke. 101
    && x.size > 10
  )

My guess is that since the second approach doesn't iterate the array multiple times, it has better performance characteristics,我的猜测是,由于第二种方法不会多次迭代数组,因此它具有更好的性能特征,

on the other hand, the first wins in readability and can be easily extended with neat git diff LOC. 另一方面,第一个在可读性方面获胜,并且可以使用简洁的 git diff LOC 轻松扩展。

  • What is better for the performance?什么对性能更好?
  • Should I choose performance or readability? 我应该选择性能还是可读性?
    (In this case, is the performance gain (if any) worth the cognitive overhead?) (在这种情况下,性能提升(如果有的话)是否值得认知开销?)

In my particular case I have a function on a Node.js server that runs 3 times per request on a set of ~40 items (after the flat() ) .在我的特殊情况下,我在 Node.js 服务器上有一个 function ,每个请求在一组 ~40 个项目上运行 3 次(在flat()之后) There is already lots of iterations and some lambda functions, so I even kind of doubt that micro-optimizing the two filters would make a difference.已经有很多迭代和一些 lambda 函数,所以我什至怀疑对这两个过滤器进行微优化会有所作为。 I believe it could all be rewritten using just a single reduce to save 7 iterations (if I count correctly), but at what cost ?我相信这一切都可以用一个reduce来重写以节省 7 次迭代(如果我计算正确的话),但代价是什么
PS: I am looking for a general answer, but you could answer my particular case as a bonus. PS:我正在寻找一个一般性的答案,但你可以回答我的特殊情况作为奖励。 Thank you.谢谢你。

export const getProductFeatures =
  (technicalSpecifications: TechnicalSpecifications, featuresBySpecLabel: FeaturesBySpecLabel): ProductFeature[] =>
    [...technicalSpecifications
      .map(specGroup => specGroup.specifications)
      .flat()
      .filter(spec => featuresBySpecLabel[spec.label] !== undefined)
      .filter(spec => verifyFeature(spec, featuresBySpecLabel[spec.label]))
      .reduce((productFeatures, spec) => {
        productFeatures.add(featuresBySpecLabel[spec.label].featureLabel)
        return productFeatures
      }, new Set<string>())]
      .map(label => ({ label }))

Based on the comments, there is no significant performance improvement for the particular case of ~40 items in an array.根据评论,对于数组中约 40 个项目的特定情况,性能没有显着提升

Technically, each additional iteration increases the time complexity n-times in worst case, so with 2 iterations O(2n) , it could take twice as long, but we are talking about time spans way down in the nano-scale, eg.从技术上讲,在最坏的情况下,每次额外的迭代都会增加 n 倍的时间复杂度,因此使用 2 次迭代O(2n) ,它可能需要两倍的时间,但我们谈论的是纳米级的时间跨度,例如。 10ns vs 20ns in this particular case.在这种特殊情况下为10ns20ns It might become worth considering depending on the size of the dataset , the machine the code is run on , etc.根据数据集的大小、运行代码的机器等,可能值得考虑。

Constants are usually ignored when computing O notation.计算O表示法时通常会忽略常量。 Ie O(n) vs O(2n) .O(n)O(2n)
~ Felix Kling (taken from comments) ~ Felix Kling (取自评论)

That being said, the performance can be taken out of the question, which now leaves only the readability factor, which is hugely subjective and opinion based .话虽如此,性能可以排除在外,现在只剩下可读性因素,这是非常主观和基于意见的 Such QAs are not allowed on SO. SO 上不允许此类 QA。 To get an answer to this, anyone wondering should probably discuss this with their team or decide by heart.为了得到这个问题的答案,任何想知道的人都应该与他们的团队讨论这个问题或用心决定。 Generally though, readability is more often than not valued more for an equivalent code, which the above is.不过,一般而言,对于等效代码,可读性往往更受重视,如上所示。 However, what anyone considers better readable depends on personal preferences or team/company guidelines.但是,任何人认为更好的可读性取决于个人喜好或团队/公司指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM