[英]Does storing in variables increase performance? And do var, let, const also affect performance?
For example, in a node app, using express, instead of using: 例如,在节点应用程序中,使用express而不是使用:
app.listen(3000);
we use: 我们用:
const port = 3000;
app.listen(port)
Will it increase performance, albeit no matter how small? 不管有多小,它都会提高性能吗? I heard, for example, it improves performance of loops. 例如,我听说它可以提高循环性能。
And off-topic: isn't app.listen() function kinda a loop? 和题外话:app.listen()函数不是一个循环吗? Or does it use an inbuilt loop? 还是使用内置循环?
Also, does declaring with var, let, or const differ from one another in performance? 此外,用var,let或const声明的性能是否彼此不同? For example, since declaring with const - instead of var or let - implies leaving the variable 'port' untouched, will it have any effect? 例如,由于用const而不是var或let进行声明意味着隐含了对变量'port'的修改,这会产生影响吗?
No, simply by storing a value in a variable does not increase the performance, in fact, there will be a slightly reduced performance since additional memory allocation and fetching happens. 不,仅通过将值存储在变量中不会提高性能,实际上,由于会发生额外的内存分配和提取,因此性能会略有降低。
However, storing the result of a computation in a variable and using the variable instead of re-computing it every time the values are required improves the performance. 但是,将计算结果存储在变量中,并在每次需要值时都使用该变量而不是重新计算它可以提高性能。
Converting app.listen(3000)
into below format does not have any performance benefit. 将app.listen(3000)
转换为以下格式没有任何性能优势。
const port = 3000;
app.listen(port)
However, if the port
values require some computation like, 但是,如果port
值需要进行类似的计算,
const port = process.env.PORT || 3000;
then storing the result in a variable and using that values every time port number is required will have a performance benefit compared to computing it every time. 然后将结果存储在变量中,并且与每次计算端口号相比,每次需要端口号时都使用该值将具有性能优势。
(In your example, since the port
value is used only once, there is no point in storing the result in a variable.) (在您的示例中,由于port
值仅使用一次,因此将结果存储在变量中毫无意义。)
I heard, for example, it improves performance of loops. 例如,我听说它可以提高循环性能。 Again, you have a performance benefit only if the value stored in the variable requires some form of computation. 同样,只有当存储在变量中的值需要某种形式的计算时,您才可以获得性能上的好处。
const array = [1,2,3];
const length = array.length;
for (let i =0 ;i<length; i++) {
console.log(array[i]);
}
The above example has performance benefits compared to the one below. 与下面的示例相比,上面的示例具有性能优势。
const array = [1,2,3];
for (let i =0 ;i<array.length; i++) {
console.log(array[i]);
}
In this example, in every iteration of the loop, the length of the array has to be calculated which is not necessary since the array does not change. 在此示例中,在循环的每次迭代中,都必须计算数组的长度,这是不必要的,因为数组不会更改。
And off-topic: isn't app.listen() function kinda a loop? 和题外话:app.listen()函数不是一个循环吗? Or does it use an inbuilt loop? 还是使用内置循环?
No, app.listen()
is not a loop. 不, app.listen()
不是循环。 It is an event listener. 它是一个事件监听器。 Internally the node event loop handles such I/O operations. 在内部,节点事件循环处理此类I / O操作。 You can read more about the event loop here . 您可以在此处阅读有关事件循环的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.