简体   繁体   中英

javascript string memory allocation

How does Javascript allocates memory for strings? I ran this simple code in chrome:

var s = 'a';
var i;
for (i = 0; i < 5000000; i++) {
    s += 'b';
}

var s2 = s;
s2[4000000] = 'a';

and attached Windbg to chrome, added conditional breakpoint in HeapAlloc, VirtualAlloc, and VirtualAllocEx with condition "requested bytes parameter > 4.5M". when the above code ran I didn't see any such big allocation in the debugger.
I expected that at least for s2 I'll see such allocation.

when the above code ran I didn't see any such big allocation in the debugger.

Here s += 'b'; has static string 'b' . It is very likely that string intering is at play.

Try making these random strings or perhaps numbers as strings.

I expected that at least for s2 I'll see such allocation.

Here s2 is just a reference ienot creating more allocation. Try copying the whole array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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