Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.tele2net.at!newsfeed.utanet.at!feeder1.cambriumusenet.nl!feed.tweaknews.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <2075691.byxZkP05Om@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sun, 02 Dec 2012 19:35:22 +0100 User-Agent: KNode/4.4.11 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+&. <5462881.fXVs31Ce0e@PointedEars.de> <5551855.fYihAxhPQP@PointedEars.de> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 118 NNTP-Posting-Date: 02 Dec 2012 19:35:22 CET NNTP-Posting-Host: 001e1de2.newsspool1.arcor-online.net X-Trace: DXC=o7<86?Z>@PR<<0iRN7DLEQic==]BZ:af^4Fo<]lROoRQnkgeX?EC@@PIRTKiQcV`1TDZm8W4\YJN\R=i2=[N6Y6Z^;HD;G0U[d^Ak9?fi>3?6] X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:17416 Cezary Tomczyk wrote: > W dniu 2012-12-02 16:11, Thomas 'PointedEars' Lahn pisze: >>> Cezary Tomczyk wrote: > [...] > Personally I do not care about IE7 and lower. Currently I am focusing on > IE8 and higher. Given the market share of the versions you do not care about, you should reconsider, as bad as that sounds to both of us. > However, thanks for explanation. You're welcome. >>> See test: http://jsperf.com/loop-for-in-vs-object-keys-foreach/2 >>> >>> And yes, I remember your opinion about those tests :-) >> >> Good. You should also know that you are comparing apples and oranges >> here. > > Maybe. It is just a overall test. Which is pointless if you do not consider that not only the functionality of the tested features is fundamentally different but also that you are working against the advantages of certain variants. Why forEach()? Why i++ where you count with j? What about ordering? >> A for-in statement is _not_ equivalent to either foreach (or the E4X `for >> each') or Object.keys(). By contrast, it iterates over own and >> *inherited* *enumerable* properties in *implementation-dependent* (in >> ES3, arbitrary) order; the iterator is the property name, not the >> property value. >> >> Object.keys() returns a reference to an Array of the names of all *own* >> enumerable properties (in the same implementation-dependent order as for >> for-in), and *only* those. [Which is why if you want to emulate >> Object.keys() for enumerable properties, you have to use >> Object.prototype.hasOwnProperty() or emulations thereof.] > > I agree with you. Just forgot to get only direct properties of object. I > use: > > " [...] > for (key in params) { > if (Object.prototype.hasOwnProperty.call(params, key)) { > [...] > " Unless you cannot be certain that the value of `params' refers to an object that has Object.prototype in its prototype chain, this is needlessly inefficient. Keep in mind that identifier lookup is a runtime feature. In each iteration `Object' will have to be resolved against the scope chain (usually up to the global object), then its `prototype' property will have to be resolved against the prototype chain, and so forth. Consider this instead: for (var key in params) { if (params.hasOwnProperty(key)) { // … } } The least you should do is to cache the value of `Object.prototype.hasOwnProperty', maybe even in a bound variable of a closure (as can be seen in JSX), if you do not call it through the prototype chain. /* * Or `Object.prototype', but `{}' appears to have a shorter * scope chain, which might win over the longer prototype chain */ var _hasOwnProperty = {}.hasOwnProperty; for (var key in params) { if (_hasOwnProperty.call(params, key)) { // … } } It might be useful to combine these: var hasHasOwnProperty = (typeof params.hasOwnProperty == "function") ? params.hasOwnProperty : {}.hasOwnProperty; for (var key in params) { if (_hasOwnProperty.call(params, key)) { // … } } That would allow the object to inherit or define its special hasOwnProperty() method, and use the built-in as fallback. > [...] >> Please trim your quotes. > > Should be better now. Yes, thanks. PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs,