简体   繁体   English

我们对 JS 中的箭头函数优化有什么保证吗?

[英]Do we have any guarantees about arrow functions optimization in JS?

Let's say we have next function:假设我们有下一个 function:

const x = a => a;
const result = x('hello')

Do we have any guarantees in Google V8 / Firefox Quantum that x will be optimized to const result = 'hello' ?我们是否在 Google V8 / Firefox Quantum 中保证x将优化为const result = 'hello'

Why I'm asking it?为什么我要问它?

Please see my answer .请看我的回答 Some times the only way to infer type in TypeScript is to make simple function.有时,在 TypeScript 中推断类型的唯一方法是制作简单的 function。


type Validation<T> = T
const x = <T>(arg:Validation<T>)=>arg

It is an overhead.这是一个开销。 So, I'm curious, can I use such kind of technique for type infering and don't worry about function overhead?所以,我很好奇,我可以使用这种技术进行类型推断并且不用担心 function 开销吗?

(V8 developer here.) (这里是 V8 开发人员。)

Generally speaking: there are no guarantees about what will or won't get optimized.一般来说:不能保证什么会或不会得到优化。 An engine's optimization decisions also change over time: the goal is not to optimize as much as possible (because optimization itself has a cost that isn't always worth it);引擎的优化决策也会随着时间而改变:目标不是尽可能地优化(因为优化本身的成本并不总是值得的); the goal is to optimize the right things at the right time.目标是在正确的时间优化正确的事情。 That may well mean that an engineering team decides to make their engine optimize a little less, for overall better performance (or less jankiness, or less memory consumption, or whatever).这很可能意味着工程团队决定让他们的引擎优化得更少,以获得更好的整体性能(或更少的抖动,或更少的 memory 消耗,或其他)。

What will likely happen in this specific case: it depends (as usual).在这种特定情况下可能会发生什么:这取决于(像往常一样)。 If that function's definition and its call site are top-level code (executed a single time), it is very likely that it won't get optimized -- because optimizing such code is usually not worth it.如果该函数的定义及其调用站点是顶级代码(执行一次),它很可能不会得到优化——因为优化这样的代码通常是不值得的。 You won't be able to measure the difference, but if you were able to measure it, you'd see that not optimizing is faster for code that runs only once.您将无法衡量差异,但如果您能够衡量差异,您会发现对于只运行一次的代码来说,不进行优化会更快。
If, on the other hand, this identity function is called from "hot" code, where that hot code itself is selected for optimization after a while, then it's very likely that the optimizing compiler will inline the function, and then (trivially) optimize it away.另一方面,如果这个标识 function 是从“热”代码中调用的,而该热代码本身会在一段时间后被选择进行优化,那么优化编译器很可能会内联 function,然后(平凡地)优化它离开。
If the definition of the identity function is executed repeatedly, then (at least before/unless inlining happens) this will unnecessarily create several function objects (because JavaScript functions have object identity). If the definition of the identity function is executed repeatedly, then (at least before/unless inlining happens) this will unnecessarily create several function objects (because JavaScript functions have object identity). That's an inefficiency that's easy to avoid (so I'd avoid it, personally);这是一种很容易避免的低效率(所以我个人会避免它); but again: whether it really matters, ie whether it has a measurable effect, depends on how often the code is executed.但同样:它是否真的重要,即它是否具有可衡量的效果,取决于代码执行的频率。

In short: it's probably fine not to worry about function overhead in such a case;简而言之:在这种情况下,不用担心 function 开销可能没问题; however there is no guarantee that calls to the identity function will get optimized away.但是,不能保证对身份 function 的调用将得到优化。

(Taking a step back, looking at the broader background: I don't know much about TypeScript's advanced features, but my gut feeling is that some sort of plugin for the TS compiler might be a more elegant way to enforce particular typechecks for literals. If strings are constructed at runtime, then TS's checks won't help anyway, just like the rest of TS's type checking system.) (退后一步,看看更广泛的背景:我对 TypeScript 的高级功能知之甚少,但我的直觉是 TS 编译器的某种插件可能是一种更优雅的方式来强制对文字进行特定类型检查。如果字符串是在运行时构造的,那么 TS 的检查无论如何都无济于事,就像 TS 的类型检查系统的 rest 一样。)

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

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