Out of memory when concatinating strings

Posted on
  • //strings.js
    
    var A="1234567890";
    var B=A;
    console.log(B);console.log(B.length);
    B=B+B;
    console.log(B);console.log(B.length);
    //out of memory if continued
    //B=B+B;
    //console.log(B);console.log(B.length);
    //B=B+B;
    //console.log(B);console.log(B.length);
    
    B=A;
    console.log(B);console.log(B.length);
    B+=B;
    console.log(B);console.log(B.length);
    B+=B;
    console.log(B);console.log(B.length);
    

    Produces:

    >echo(0);
    1234567890
    10
    12345678901234567890
    20
    1234567890
    10
    12345678901234567890
    20
    ERROR: Out of Memory!
    WARNING: Out of memory while appending to array
    Execution Interrupted
    =undefined
    >
    
  • This works

    var A="1234567890";
    var B = A;
    console.log(B.length,process.memory());
    B+=A;
    console.log(B.length,process.memory());
    B+=A;
    console.log(B.length,process.memory());
    

    Looks like a problem with iterating through a string that is changed during iteration.

  • Interesting - thanks! I'll make an issue for this.

    There was a post a few weeks ago where someone asked whether += was optimised to append. This looks like a side-effect of that optimisation.

  • Actually just fixed this - it was an easy tweak

  • Thanks Gordon, glad it was not a major rewrite.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Out of memory when concatinating strings

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions