Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!news.internetdienste.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1493417.ljR73ylGCY@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 18 Apr 2013 15:09:56 +0200 User-Agent: KNode/4.8.5 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. MIME-Version: 1.0 Lines: 90 NNTP-Posting-Date: 18 Apr 2013 15:09:59 CEST NNTP-Posting-Host: bd27c3fa.newsspool1.arcor-online.net X-Trace: DXC=>3fbHZ@0dDj:i=48;n?Z:`ic==]BZ:afn4Fo<]lROoRankgeX?EC@@`E^]7Dg`Z:QjDZm8W4\YJNlb@mF9jNikdeIQbgJMhLo0gmh>fP9m7jNk X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:19335 Tony wrote: > I found the solution You have found *a* solution; not the best one. Not even a good one. > var i = remArray.length; > while (i--) > { > if (remArray[i] != undefined) Although “undefined” can be considered a safe *feature* now, you may want to use if (typeof remArray[i] != "undefined") for maximum backwards-compatibility and value-safety (in previous implementations, “undefined” could be overwritten if it was supported). > { > hinderArray.splice(remArray[i], 1); ^^^^^^^^^^^ Avoid inefficient repeated MemberExpressions for the same value; use variables. > } > } This removes all elements that are not already undefined or null from the array, not just those where the “Enable” property (should be “enable”, BTW) is “false”. Anyhow, if there *many* items to be removed (this was your premise), or if the array is large, this approach is inefficient, because the array is reorganized in each step, that is – \ index | 0 | 1 | 2 | 3 step \ | | | | ------------+-----------+-------+-------+----------- 0 | "foo" | "bar" | "baz" | "bla" 1 | [deleted] | "bar" | "baz" | "bla" 2 | "bar" | "bar" | "baz" | "bla" 3 | "bar" | "baz" | "baz" | "bla" 4 | "bar" | "baz" | "bla" | "bla" 5 | "bar" | "baz" | "bla" | [deleted] – if only "foo" is to be removed. *Five* steps for removing *one* element if three elements follow it. It is much more efficient then (both memory-wise and runtime-wise) to create a new Array instance and fill it only with the values that match: var a = []; for (var i = 0, len = hinderArray.length; i < len; ++i) { var item = hinderArray[i]; /* item could be null */ if (item && item.Enable) { a.push(item); } } hinderArray = a; or, with ECMAScript Edition 5 and later: hinderArray = hinderArray.filter(function (item) { return item && item.Enable; }); The “filter” method is more efficient with sparse arrays (because it only operates on defined elements), but probably less efficient (due to repeated callback calls) with other ones. JSX:array.js includes a (not yet unit- tested) implementation, jsx.array.filter(), that by default augments Array.prototype: > [Top post] Please don't. -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.