Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Asen Bozhilov Newsgroups: comp.lang.javascript Subject: Re: Empty array Date: Wed, 29 Feb 2012 01:23:03 -0800 (PST) Organization: http://groups.google.com Lines: 54 Message-ID: <3762265.125.1330507383456.JavaMail.geo-discussion-forums@ynil17> References: <0f481e7c-97b9-4c76-9660-321681876658@w9g2000vbv.googlegroups.com> NNTP-Posting-Host: 84.40.76.49 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1330507475 15039 127.0.0.1 (29 Feb 2012 09:24:35 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 29 Feb 2012 09:24:35 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=84.40.76.49; posting-account=7Fm93QoAAADJLfahesQYQvMxF-JssE8W User-Agent: G2/1.0 X-Received-Bytes: 3235 Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:12270 Gene Wirchenko =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0: > The emptiness check could apparently be >=20 > var fEmpty=3Dtrue; > for (Item in theArray) > { > fEmpty=3Dfalse; > break; > } >=20 > I checked this, and it works, but is there a better way? Entering a > loop and breaking out of it unconditionally is perverse. Actually this is not a check for an emptiness. That checks for enumerable p= roperties, which is different than the OP's question. =20 Firstable we have to consider what is an empty object in ECMAScript. By def= ault every native object inherits from `Object.prototype` and the object is= not empty. The only one exception is:=20 var empty =3D Object.create(null); =20 With `Object.create` you can create proto-less object, which does not inher= it properties from anywhere. That object can be consider as an empty object= . ECMAScript 5 provides API for manipulating the internal states of the prope= rties. We are able to create non-enumerable properties, which means the che= cking for emptiness with `for-in` will not return correct result. =20 As I wrote, there are no reasons to use sparse arrays or arrays with no int= eger properties. In both cases a native object would be sufficient. The arr= ays are usable when they are sequenced collection.=20 When I expect somewhere an array in my program I do the following to extrac= t its elements.=20 for (var i =3D 0, len =3D arr.length; i < len; i++) { // do something with arr[i] element }=20 By this, checking for emptiness of array is achieved via its `length' prope= rty. This is the simpliest and efficient way.=20 Checking for emptiness of sparse arrays and objects could be achived with c= ustom setter and delete methods. For examle if you use proxy object you can= use counter for the properties. In the setter if there is not the used pro= perty you should increment the counter and in delete the opposite operation= . =20 =20