• <bdo id='evowi'></bdo><ul id='evowi'></ul>
  • <small id='evowi'></small><noframes id='evowi'>

    <legend id='evowi'><style id='evowi'><dir id='evowi'><q id='evowi'></q></dir></style></legend>
    <tfoot id='evowi'></tfoot>

  • <i id='evowi'><tr id='evowi'><dt id='evowi'><q id='evowi'><span id='evowi'><b id='evowi'><form id='evowi'><ins id='evowi'></ins><ul id='evowi'></ul><sub id='evowi'></sub></form><legend id='evowi'></legend><bdo id='evowi'><pre id='evowi'><center id='evowi'></center></pre></bdo></b><th id='evowi'></th></span></q></dt></tr></i><div id='evowi'><tfoot id='evowi'></tfoot><dl id='evowi'><fieldset id='evowi'></fieldset></dl></div>

        如何强制 JavaScript 深度复制字符串?

        时间:2023-10-12
          <tbody id='lavp9'></tbody>

            <i id='lavp9'><tr id='lavp9'><dt id='lavp9'><q id='lavp9'><span id='lavp9'><b id='lavp9'><form id='lavp9'><ins id='lavp9'></ins><ul id='lavp9'></ul><sub id='lavp9'></sub></form><legend id='lavp9'></legend><bdo id='lavp9'><pre id='lavp9'><center id='lavp9'></center></pre></bdo></b><th id='lavp9'></th></span></q></dt></tr></i><div id='lavp9'><tfoot id='lavp9'></tfoot><dl id='lavp9'><fieldset id='lavp9'></fieldset></dl></div>
            <tfoot id='lavp9'></tfoot>

              • <bdo id='lavp9'></bdo><ul id='lavp9'></ul>
                <legend id='lavp9'><style id='lavp9'><dir id='lavp9'><q id='lavp9'></q></dir></style></legend>

                <small id='lavp9'></small><noframes id='lavp9'>

                1. 本文介绍了如何强制 JavaScript 深度复制字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一些看起来像这样的 javascript 代码:

                  var myClass = {编号:{}myFunc:函数(巨大字符串){var id = huge_string.substr(0,2);ids[id] = 真;}}

                  稍后,该函数被一些大字符串 (100 MB+) 调用.我只想保存在每个字符串中找到的短 id.但是,谷歌浏览器的子字符串函数(实际上是我的代码中的正则表达式)只返回一个切片字符串"对象,它引用了原始对象.因此,在对 myFunc 的一系列调用之后,我的 chrome 选项卡内存不足,因为临时 huge_string 对象无法被垃圾回收.

                  如何复制字符串 id 以便不维护对 huge_string 的引用,并且 huge_string 可以垃圾收集了吗?

                  解决方案

                  JavaScript 的 ECMAScript 实现因浏览器而异,但对于 Chrome,许多字符串操作(substr、slice、regex 等)只是保留对原始字符串,而不是复制字符串.这是 Chrome 中的一个已知问题(

                  I have some javascript code which looks like this:

                  var myClass = {
                    ids: {}
                    myFunc: function(huge_string) {
                       var id = huge_string.substr(0,2);
                       ids[id] = true;
                    }
                  }
                  

                  Later the function gets called with some large strings (100 MB+). I only want to save a short id which I find in each string. However, the Google Chrome's substring function (actually regex in my code) only returns a "sliced string" object, which references the original. So after a series of calls to myFunc, my chrome tab runs out of memory because the temporary huge_string objects are not able to be garbage collected.

                  How can I make a copy of the string id so that a reference to the huge_string is not maintained, and the huge_string can be garbage collected?

                  解决方案

                  JavaScript's implementation of ECMAScript can vary from browser to browser, however for Chrome, many string operations (substr, slice, regex, etc.) simply retain references to the original string rather than making copies of the string. This is a known issue in Chrome (Bug #2869). To force a copy of the string, the following code works:

                  var string_copy = (' ' + original_string).slice(1);
                  

                  This code works by appending a space to the front of the string. This concatenation results in a string copy in Chrome's implementation. Then the substring after the space can be referenced.

                  This problem with the solution has been recreated here: http://jsfiddle.net/ouvv4kbs/1/

                  WARNING: takes a long time to load, open Chrome debug console to see a progress printout.

                  // We would expect this program to use ~1 MB of memory, however taking
                  // a Heap Snapshot will show that this program uses ~100 MB of memory.
                  // If the processed data size is increased to ~1 GB, the Chrome tab
                  // will crash due to running out of memory.
                  
                  function randomString(length) {
                    var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
                    var result = '';
                    for (var i = 0; i < length; i++) {
                      result +=
                          alphabet[Math.round(Math.random() * (alphabet.length - 1))];
                    }
                    return result;
                  };
                  
                  var substrings = [];
                  var extractSubstring = function(huge_string) {
                    var substring = huge_string.substr(0, 100 * 1000 /* 100 KB */);
                    // Uncommenting this line will force a copy of the string and allow
                    // the unused memory to be garbage collected
                    // substring = (' ' + substring).slice(1);
                    substrings.push(substring);
                  };
                  
                  // Process 100 MB of data, but only keep 1 MB.
                  for (var i =  0; i < 10; i++) {
                    console.log(10 * (i + 1) + 'MB processed');
                    var huge_string = randomString(10 * 1000 * 1000 /* 10 MB */);
                    extractSubstring(huge_string);
                  }
                  
                  // Do something which will keep a reference to substrings around and
                  // prevent it from being garbage collected.
                  setInterval(function() {
                    var i = Math.round(Math.random() * (substrings.length - 1));
                    document.body.innerHTML = substrings[i].substr(0, 10);
                  }, 2000);
                  

                  这篇关于如何强制 JavaScript 深度复制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何获取 javascript 对象引用或引用计数? 下一篇:我可以触发 JavaScript 的垃圾回收吗?

                  相关文章

                  最新文章

                    1. <legend id='WpHjl'><style id='WpHjl'><dir id='WpHjl'><q id='WpHjl'></q></dir></style></legend>

                      <i id='WpHjl'><tr id='WpHjl'><dt id='WpHjl'><q id='WpHjl'><span id='WpHjl'><b id='WpHjl'><form id='WpHjl'><ins id='WpHjl'></ins><ul id='WpHjl'></ul><sub id='WpHjl'></sub></form><legend id='WpHjl'></legend><bdo id='WpHjl'><pre id='WpHjl'><center id='WpHjl'></center></pre></bdo></b><th id='WpHjl'></th></span></q></dt></tr></i><div id='WpHjl'><tfoot id='WpHjl'></tfoot><dl id='WpHjl'><fieldset id='WpHjl'></fieldset></dl></div>

                      <small id='WpHjl'></small><noframes id='WpHjl'>

                      <tfoot id='WpHjl'></tfoot>
                      • <bdo id='WpHjl'></bdo><ul id='WpHjl'></ul>