JS performance Aspect--memory management and new attribute method of ECMAScript5 object

Source: Internet
Author: User

Delete the property of an object slows this object (15 times times more memory is consumed)
var o = {x: ' Y ' }; Delete // at this point O will become a slow object // var o = {x: ' y 'null;  // it should be .

Closed Package

When a variable outside the closure is introduced into the closure, the object cannot be garbage collected (GC) when the closure ends.

var function () {  varnew Array (1000000). Join (' x ');   return function () {    return  largestr;  }} ();

Dom leaks

When the original COM is removed, the child node references are not removed and cannot be reclaimed.

varselect =Document.queryselector;varTreeref = Select (' #tree '); //Leafref is a sub-node of treefre in the COM treevarLeafref = Select (' #leaf '); varBODY = Select (' Body '); Body.removechild (TREEREF); //#tree不能被回收入, because Treeref is still here.//Workaround:Treeref =NULL; //The tree cannot be recycled, because the leaf results leafref stillLeafref =NULL; //now the #tree can be released. 

Timers (fixed) time device leakage
 for (var i = 0; i < 90000; i++) {  var buggyobject = {    function() {       var  This ;       var val = setTimeout (function() {        ref.callagain ();       90000);    }  }   Buggyobject.callagain ();   // Although you want to recycle, but the  timer's still there. NULL ;}

Debug memory

Chrome's own memory debugging tool makes it easy to see memory usage and memory leaks:

Click on the record in Timeline-I:

Object.create (Prototype[,descriptors])

This method is used to create an object, assign its prototype property to the first parameter, and can set multiple descriptors, about Decriptor the next method will be described here first not to say. That's all you need to create a prototype chain clean object.

var o = object.create ({            function  () {                alert (this. Name);            } ,            "name": "Byron"        });

Object.defineproperty (O,prop,descriptor)/object.defineproperties (o,descriptors)

To understand that these two functions must understand what descriptor is, the Object field in the previous JavaScript is an object property, a key-value pair, and a few features introduced in the ECMASCRIPT5 Property,property

1. Value: values, default is undefined

2. Writable: Read-only property, default is False, a bit like the const in C #

3. Enumerable: Whether it can be enumerated (for in), false by default

4. Configurable: Can be deleted, default false

Same as C #, Java Get/set, but these two can not be used in conjunction with value, writable

5.get: A worthwhile way to return the property, default is undefined

6.set: The method of setting a value for the property, default is undefined

Object.defineproperty (o, ' age ', {value:24, writable:true, Enumerable:true, Configurable:true        }); Object.defineproperty (O,' Sex ', {value:' Male ', writable:false, Enumerable:false, Configurable:false        }); Console.log (O.age); // -O.age = 25;  for(varObjincho) {console.log (obj+ ': ' +O[obj]); /*age:25//No Sex:male traversed say:function () {alert (this.name); } Name:byron*/        }        DeleteO.age; Console.log (O.age); //undefinedConsole.log (o.sex);//male        //o.sex = ' female ';//cannot assign to read the property ' sex ' of #<object>        DeleteO.age; Console.log (O.sex); //male, and has not been removed

You can also use the Defineproperties method to define multiple property at the same time

object.defineproperties (o, {' Age ': {value:24, writable:true, Enumerable:true, Configurable:true            },            ' Sex ': {value:' Male ', writable:false, Enumerable:false, Configurable:false            }        });

Object.getownpropertydescriptor (O,property)

This method is used to obtain the property attribute of the DefineProperty method setting

var props = Object.getownpropertydescriptor (o, ' age '//Object {value:24, writable:true , Enumerable:true, configurable:true}

Object.getownpropertynames

Gets all the property names, not including the properties in Prototy, and returns an array

// ["Age", "sex"]
If you feel this blog post to you have a harvest, think the little woman is still hard, please click on the bottom right corner of the [recommended], Thank you!

JS performance Aspect--memory management and new attribute method of ECMAScript5 object

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.