for own(...) loop (spin-off from Re:, hasOwnProperty(), and inheritance)

Brendan Eich brendan at
Tue Nov 8 15:36:34 PST 2011

On Nov 8, 2011, at 2:26 PM, John J Barton wrote:

> On Tue, Nov 8, 2011 at 1:45 PM, Timmy Willison <timmywillisn at> wrote:
>> Yes, I think JS needs something like this.  This is new to no one here, but
>> my main concern with using hasOwnProperty is the loss of performance, so I
>> think it would be worth exploring more than just syntactic sugar.  But I
>> don't know how realistic it is to hope for that.
> Added case Object.keys().forEach:
> Of course results depend more on browser than form of the loop...

54 properties per object, if my skimming is accurate (is it?). Not enough to show the difference in keys allocating an array vs. some internal snapshot or iterator used by

Lots of confounding variables here. Firefox 8 wins top prize but only for the for-in loop without hasOwn guard. FF9 has type inference, so may do better still. More work on inlining should level the scores.

But this kind of performance is an ongoing arms race, and it's usually wrong to bend your coding style around particular results -- especially for small objects where the keys array allocation, and even a not-inlined function expression call, do not matter. POitRoaE.

Ignoring performance, a lot of stylish JS hackers use Object.keys(o).forEach. How many run into the wrong |this| (arguments/break/continue/return)? Not clear. Something to study.

Finally, there ain't just one way to say it in JS. We'll have Object.keys(o).forEach(function(v,i){...}) *and* (in for (i of keys(o))... or for ([k,v] of items(o))... given the module imports or a standard prelude.


More information about the es-discuss mailing list