简体   繁体   English

我是否提高了性能我在 JavaScript 中声明了繁忙的 function 之外的变量?

[英]Am I improving performance I declare a variable outside of busy function in JavaScript?

Imagine I have a function that is being called very often and needs to use a variable internally.想象一下,我有一个经常被调用的 function,需要在内部使用一个变量。

function busyFunction() {
    var intermediateResult;
    /* compute something */
    return something;
}

As I understand, in this first example the browser will allocate memory for the variable and then schedule it for garbage collection at some point.据我了解,在第一个示例中,浏览器将为变量分配 memory,然后安排它在某个时候进行垃圾收集。

var intermediateResult;
function busyFunction() {
    /* compute something */
    return something;
}

I know the second example will pollute the scope outside of busyFunction.我知道第二个例子会污染busyFunction之外的scope。 But since the the memory for the variable would not be garbage collected until the parent function is, would this be beneficial for performance?但是由于变量的 memory 在父 function 之前不会被垃圾收集,这对性能有好处吗? If I'm wrong here or if the effect is negligible, I'd rather use the first cleaner example.如果我在这里错了或者效果可以忽略不计,我宁愿使用第一个更干净的例子。

In short, yes.简而言之,是的。 You consume less resources, because every time the function gets executed you don't create a variable in it's scope that then gets thrown away after the function execution ends.您消耗的资源更少,因为每次执行 function 时,您都不会在 scope 中创建变量,然后在 function 执行结束后将其丢弃。 Personally I'd advise to create an object which holds both the variable and the function so that the variable doesn't just "float around", but that's detail.就我个人而言,我建议创建一个 object 来保存变量和 function 以便变量不只是“浮动”,但这是细节。

Another thing however is code readability.然而,另一件事是代码可读性。 I presume the example in the question is a simplified version of what you're dealing with.我认为问题中的示例是您正在处理的内容的简化版本。 Here it basically comes down to balance between readability and efficiency.在这里,它基本上归结为可读性和效率之间的平衡。 If you think it would be confusing to leave it outside then leave it in the function unless the advantages of having it outside heavily outweigh the fact that the code is less confusing.如果您认为将其放在外面会令人困惑,那么请将其留在 function 中,除非将其放在外面的优势大大超过了代码不那么令人困惑的事实。

Unless the performance improvement is very significant I'd advise to leave it in the function for clarity.除非性能改进非常显着,否则为了清楚起见,我建议将其留在 function 中。

Declaring variables in a higher scope makes them a bit slower to access (very so in the global scope), and - more importantly - harder to optimise for the compiler.在更高的 scope 中声明变量会使它们的访问速度变慢(在全局范围内非常如此),并且 - 更重要的是 - 更难为编译器优化。 The garbage collection cost is negligible.垃圾收集成本可以忽略不计。 I will expect this to actually be slower.我预计这实际上会更慢。 The difference should be quite small (hardly measurable), but as always, benchmark it yourself on your actual code with real data.差异应该非常小(几乎无法测量),但与往常一样,请在您的实际代码中使用真实数据自行对其进行基准测试。

The only case where the performance would improve is if you want to persist a value between the invocations, and would gain your advantage by not repeatedly running costly initialisation code.性能会提高的唯一情况是,如果您想在调用之间保持一个值,并且通过不重复运行昂贵的初始化代码来获得优势。 Especially if you can make the variable a const ant initialised before the function is called.特别是如果您可以将变量设置为const ant 在调用 function 之前初始化。

In general, aim for readable and idiomatic code , optimising compilers do as well.一般来说,以可读和惯用的代码为目标,优化编译器也是如此。

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

相关问题 我应该在函数内部还是外部声明变量? - Should I declare a variable inside a function or outside? 有没有办法可以在 function 之外声明变量? - Is there a way that I can declare the variable outside function? 我无法访问 function 之外的局部变量 - I am not able to access local variable outside function 如何在javascript函数中声明PHP变量? - How do I declare a PHP variable inside a javascript function? JavaScript:是否可以通过查询调用哪个函数来声明变量? (新手) - JavaScript: Can I declare a variable by querying which function is called? (Newbie) 我无法访问在函数范围之外在函数中执行操作的全局声明变量 - I am not able to access the globally declared variable, on which I am performing an operation in a function, outside the function scope jQuery / Javascript:如何获取匿名函数以引用外部变量? - jQuery/Javascript: How to get anonymous function to reference variable declare outside? 我需要访问它当前所在的 function 之外的变量。(JavaScript 和 NodeJS) - I need to access a variable outside of the function it is currently in. (JavaScript & NodeJS) Javascript。 如何访问 function 之外的变量? - Javascript. How can I access a variable outside of a function? 如何在 javascript 中的 function 之外创建 email 变量 - How do I create email variable outside of function in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM