String memory optimization (embedded in the browser)

Source: Internet
Author: User

Code 1:
VaR array = [];
For (VAR I = 0; I <100000; I ++ ){
Array. Push ({name: "keatkeata "});
}
Code 2:
VaR array = [];
For (VAR I = 0; I <100000; I ++ ){
VaR OBJ = new object ();
VaR STR = new string ("keatkeata ");
OBJ. Name = STR;
Array. Push (OBJ );
OBJ = NULL;
}

The results of code 1 and Code 2 are the same.
But code 1 uses the memory of 6.xm and Code 2 uses 10 m.
Why?
In fact, this is similar to the principle of C.
STR is also an object that can also be represented by a pointer.
So code 1 is actually a new string ("keatkeat") once, and all subsequent references to the same object
Code 2, which is a new string many times, consumes a lot of memory!
JSON. parse uses 2nd methods, so you will find that there is not much consumption, and the result is more in JSON. parse!
Of course, if your strings are different, the browser won't be able to be optimized. Please be careful!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.