简体   繁体   English

JavaScript中的大变量-使它们成为全局变量?

[英]Big variables in JavaScript - make them global?

In a fairly large application made in Cordova, we keep going back and forth taking a set of variables from the user's LocalStorage. 在Cordova开发的一个相当大的应用程序中,我们不断地从用户的LocalStorage中获取一组变量。 I mean, this set is quite big (in can go from some kbytes to fairly 10mb). 我的意思是,这个集合很大(可以从几KB到10mb)。 I'm pretty sure that keeping variables loaded in memory may not be a pretty good idea (especially in low power, low memory devices), but it turns out that getting them from the LocalStorage looks like it's taking too long. 我非常确定,将变量加载到内存中可能不是一个好主意(尤其是在低功耗,低内存的设备中),但是事实证明,从LocalStorage中获取变量似乎花费了太长时间。 At this moment, our practice is something like this: 目前,我们的做法是这样的:

function doSomething() {
    var ourData = JSON.parse(localStorage.getItem('ourData'));

    ourData = processingBackAndForth(ourData);

    localStorage.setItem('ourData', JSON.stringify(ourData));

    syncWithServer(ourData);
}

Where processingBackAndForth(ourData) is pretty intensive and the whole function is used almost once per 10 seconds. 其中processingBackAndForth(ourData)非常密集,并且整个功能几乎每10秒使用一次。 Now, I was thinking, should this be faster? 现在,我在想,这应该更快吗?

ourData = JSON.parse(localStorage.getItem('ourData'));

function doSomething() {
    ourData = processingBackAndForth(ourData);

    localStorage.setItem('ourData', JSON.stringify(ourData));
    syncWithServer(ourData);
}

After reading a little on this , well, it basically tells that it's better to keep them in the local scope (which I know is better for encapsulation), but isn't this process a little too much? 对此进行了一点阅读之后,它基本上表明将它们保留在本地范围内更好(我知道这对于封装来说更好),但是这个过程不是太多了吗? Just picture it, retrieving from LocalStorage, saving it back again, and letting GC to clean a variable with will be used afterwards once again. 只需对其进行描绘,然后从LocalStorage检索,然后再次保存,然后再使用GC清理变量即可。

I know one of the points that states the link is that if something needs to be used across the app, and only in really special cases, then make it global, but anyway things like keeping a synced version between the server and LocalStorage still concern a lot to me, so actually I only remove the weight of retriving and garbage collecting. 我知道指出链接的要点之一是,如果需要在整个应用程序中使用某些东西,并且仅在非常特殊的情况下才将其设为全局,但是无论如何,保持服务器和LocalStorage之间的同步版本仍然需要考虑对我来说非常重要,所以实际上我只减轻了回收和垃圾收集的负担。

Would a JavaScript code use a big global variable instead of big local variables to gain some micro-optimizations? JavaScript代码会使用较大的全局变量而不是较大的局部变量来获得一些微优化吗?

You say that your data can be really large (up to 10MB). 您说您的数据可能非常大(最大10MB)。 Now the question is: would it be better to retrieve your data only one time and save it globally? 现在的问题是: 是否最好只检索一次数据并将其全局保存?

In terms of performance, if you get or set your data many times this will really slow down your program, hence your goal is to request and set the data as few times as possible , so that you will not have to wait for the localStorage to retrieve it every time. 在性能方面,如果您多次获取或设置数据,这确实会减慢程序运行速度,因此,您的目标是尽可能少请求和设置数据 ,从而不必等待localStorage延迟。每次都检索它。

Now, given your function: 现在,给定您的功能:

ourData = JSON.parse(localStorage.getItem('ourData'));

function doSomething() {
    ourData = processingBackAndForth(ourData);

    localStorage.setItem('ourData', JSON.stringify(ourData));
    syncWithServer(ourData);
}

The above code would be the best solution if you call the doSomething() function multiple times , because the data is only retrieved once, then can be elaborated as many times as you want, and saved again. 如果您多次调用doSomething()函数上述代码将是最佳的解决方案 ,因为该数据仅被检索一次,然后可以根据需要进行多次精加工,然后再次保存。 When you have finished using your data you should just delete it ( delete ourData; ). 使用完数据后,只需删除它( delete ourData; )。

Otherwise, if you call the doSomething() function only one time, then the best solution would be to get the data inside the function itself , since that it will only live as long as the function runs, and it will be auto-removed from the memory once the function ends, so the best way to do it would be: 否则, 如果只调用一次doSomething()函数,那么最好的解决方案是在函数本身内部获取数据 ,因为该数据仅在该函数运行时才存在,并且会自动从该函数中删除。函数结束后将其存储在内存中,因此最好的方法是:

function doSomething() {
    var ourData = JSON.parse(localStorage.getItem('ourData'));
    ourData = processingBackAndForth(ourData);

    localStorage.setItem('ourData', JSON.stringify(ourData));
    syncWithServer(ourData);
}

TL;DR you've got two options: TL; DR,您有两种选择:

  1. If you use the doSomething() function several times , it's better to save the data globally and then delete it when finished. 如果多次使用doSomething()函数,最好先全局保存数据,然后在完成后将其删除。
  2. If you use the doSomething() function only one time , then it's better to retrieve the data inside the function, using the var keyword. 如果只使用一次 doSomething()函数,则最好使用var关键字在函数内部检索数据。

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

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