Javascript Web 应用程序中的内存与速度
在我以前的Web应用程序中,当用户从一个“页面”(没有页面重新加载,只是一个新的div)转换到另一个“页面”时,我只是隐藏了一个div以供以后使用,并创建+显示了新的div。当用户从地址管理返回到事件管理时,我只需要隐藏当前的div并重新显示已经使用过的div即可。当然,这需要内存,但速度更快。
在我的新 Web 应用程序中,我使用 Backbone.js、Require.js 和 jQuery。我的所有模块都是AMD的(jquery 1.7.1,backbone.js 0.5.3-optamd3,...)。
阅读 Derick Bailey 的有趣博客 (http:// lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/)现在,我在转换到新“页面”之前清理我的 div,并重新创建它,以防用户返回它。
同样,对于 requirejs amd 模块,我曾经有一个速度超过内存的策略:我的 Web 应用程序导航的核心位于我唯一的路由器对象中。如果用户第一次选择“页面”/功能,我会使用 require 命令加载它的 amd 模块(它是backbone.js 视图对象)及其所有依赖项,并存储此结果视图对象(及其模型对象) ) 以便稍后在路由器对象的数组中使用。当用户回来时,我获取存储的视图对象并重新渲染视图。
我想我也会从这种行为切换到总是重新加载模块(从缓存),但我不确定。
为了走最好的路,我想得到更好的理解,想问2个问题:
- 我有5个AMD模块。当用户需要某个功能时,我加载并执行一个模块并获得一个 Backbone.js 视图对象作为结果,我将其存储在路由器对象的数组中。每个 AMD 模块都有 Backbone.js(AMD 版本)作为依赖项。当用户访问了所有 5 个“页面”并且我的所有 5 个视图对象都存储在我的数组中时,我的浏览器内存中是否有 5 个backbone.js 副本,因为每个backbone.js 依赖项都是从缓存中获取并重新执行的,或者垃圾收集器已将其删除吗?
- 其他 Web 应用程序开发人员如何看待这种速度优于内存的策略?
持续 今天我在stackoverflow上发现了类似的问题(http://stackoverflow.com/questions/7866971/how-does-amd-specially-requirejs-handle-dependancies-across-multiple-module)。 答案是:“它只会加载一次,上述两个模块将获得相同的模块值......”。
因此,存储已加载+执行的 amd 模块的结果以供以后使用似乎并没有那么糟糕。
沃尔夫冈
In my former web applications, when the user had a transition from one "page" (no page reload, just a new div) to another, I just hid the one div for later use and created + showed the new one. When the user returned from address management to events management, I only needed to hide the current div and re-show the already used one. Of course, this needs memory, but is faster.
In my new web application, I use Backbone.js, Require.js and jQuery. All my modules are AMD (jquery 1.7.1, backbone.js 0.5.3-optamd3, ...).
After reading Derick Bailey's interesting blogs (http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/) I now clean my divs before having a transition to a new "page" and re-create it in case the user returns to it.
Likewise, regarding requirejs amd modules I used to have a speed over memory strategy: The heart of my web app navigation is in my only router object. If the user selects a "page"/feature for the first time, I load the amd module (it's a backbone.js view object) for it and all its dependencies with the require command and store this resulting view object (with its model object) for later use in an array in the router object. When the user comes back, I take the the stored view object and re-render the view.
I guess I will switch from this behaviour also into always reloading the module (from cache), but I am unsure.
In order to go the best way, I want to get a better understanding and want to ask 2 questions:
- I have 5 AMD modules. When the user needs a feature, I load and execute a module and get a backbone.js view object as a result, which I store in an array in my router object. Every AMD module has Backbone.js (AMD version) as a dependency. When the user has visited all 5 "pages" and all my 5 view objects are stored in my array, do I have 5 copies of backbone.js in my browser memory since every backbone.js dependency is fetched from cache and executed anew, or has the garbage collector removed it?
- How do other web application developer think about this speed over memory strategy?
CONTINUED
Today I found a similar question on stackoverflow (http://stackoverflow.com/questions/7866971/how-does-amd-specifically-requirejs-handle-dependancies-across-multiple-module).
The answer was: "It will only be loaded once, both of the above modules will get the same module value ...".
So it seems that it is not so bad to store the results of already loaded+executed amd modules for later use.
Wolfgang
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
缓存所有内容(可能会多次使用)。 (写入 Canvas/ImageData)。
内存中应该只有 1 个框架副本。如果您担心还有更多,请重写它以强制所有 AMD 使用单一源 Backbone。
内存就是速度。
如果你想要更好的速度:
Cache everything (that might be used more than once) . (write to Canvas/ImageData).
You should only have 1 copy of the framework in memory. If you're concerned there's more, rewrite it to force all AMDs to use a single source Backbone.
Memory is speed.
If you want better speed: