简体   繁体   English

Javascript单页应用程序和脚本管理

[英]Javascript single page application and script management

I'm building a single page application with vanilla JS + Knockout JS. 我正在使用Vanilla JS + Knockout JS构建一个单页应用程序。 The application will consist of multiple sub-applications which I would like to dynamically load (and subsequently unload). 该应用程序将由多个子应用程序组成,我想动态加载(然后卸载)。 The problem is that while I can add and execute a new script with the following: 问题是,尽管我可以使用以下命令添加和执行新脚本:

function loadJs(url, hash){
    var fileObj=document.createElement('script');
    fileObj.setAttribute('type','text/javascript');
    fileObj.setAttribute('src', url);

    if (hash != undefined) {
        fileObj.setAttribute('integrity', hash);
    }

    fileObj.setAttribute('crossorigin', 'anonymous')
    document.getElementsByTagName('head')[0].appendChild(fileObj);
}

I cannot remove (including from memory) with the following: 我无法使用以下方法删除(包括从内存中删除):

function unloadJs(url){
    var allScripts = Array.from(document.getElementsByTagName('script'));

    allScripts.forEach( script => {
        if (script.src == url) {
            script.remove();
        }
    });
}

The remove script only removes the tag, but the code is still in memory. 删除脚本仅删除标签,但是代码仍在内存中。 Based on this, it seems like I should just load all the application scripts when the application is initially opened rather than managing them dynamically. 基于此,似乎我应该只在最初打开应用程序时加载所有应用程序脚本,而不是动态管理它们。 In this method, I could combine and minify the script into a single file. 在这种方法中,我可以将脚本合并并缩小到一个文件中。 I was hoping to minimize browser memory usage and prevent leaks. 我希望最大程度地减少浏览器的内存使用并防止泄漏。

I read a few SO answers discussing closures and code leaving memory automatically when no longer referenced, but I couldn't establish exactly what the examples were showing and whether my time was well invested to specifically understand closures. 我读了一些SO答案,讨论了不再需要关闭的闭包和代码自动离开内存的问题,但是我无法确切确定示例显示的内容以及我是否花了很多时间专门了解闭包。

The Javascript code is only handling the UI, and there is minimal data manipulation. Javascript代码仅处理UI,并且几乎没有数据操作。 The major work will be done on the server with JS displaying the results. JS显示结果将在服务器上完成主要工作。 Is this type of thing simply premature optimization? 这类事情是否只是过早的优化?

As you have found, removing the script tag does not automatically remove the code or variables from memory. 如您所知,删除脚本标记不会自动从内存中删除代码或变量。 It might eventually get collected eventually, but it's not something you can guarantee. 它可能最终会最终被收集,但这不是您可以保证的。 In my experience, they stick around for a while. 根据我的经验,他们会停留一段时间。

I would suggest loading your scripts into a minified JS file and loading them in advance like you describe. 我建议您将脚本加载到缩小的JS文件中,并按照您的描述提前加载它们。

If you're concerned about memory use, you could externalize each set of mini-apps' scripts into a JS object so that all the objects and functions are part of one single parent object for each mini-app (sort of a namespace). 如果您担心内存的使用,则可以将每组微型应用程序的脚本外部化为一个JS对象,以便所有对象和功能都成为每个微型应用程序(一个命名空间)的单个父对象的一部分。 You would create a new isntance for that specific page/app's object when it's loaded, and then you can destroy/delete that object when it is unloaded. 您将在加载该特定页面/应用程序的对象时为其创建一个新的对象,然后可以在该对象卸载时销毁/删除该对象。 That would tell the garbage collector that the object can be removed. 这将告诉垃圾收集器可以删除该对象。

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

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