Re: [nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-14 Thread Axel Kittenberger
I think this is a good example for, that haziness of performance optimization in a script language like JS. What may be efficient to the engine today, might not so be in future and vice verca. We'll just have to live with it. I remember the older days of JS, where JS-engines were so slow, anything

Re: [nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-14 Thread Tim Caswell
Also note that performance changes over time. Last time I benchmarked for..in vs Object.keys was many V8 versions ago. They may have optimized for..in more since then. One very important thing to remember in benchmarks, especially micro-benchmarks like this is that your loop is probably not your

Re: [nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-14 Thread Scott González
On Tue, Feb 14, 2012 at 3:25 AM, Jimb Esser wrote: > I keep reading things saying "Object.keys is much faster than > for..in", but in most tests I do (and in a bunch I find on > jsperf.com), that is the opposite of the case. > There are two big differences: 1) for..in walks the prototype chain,

[nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-14 Thread Jimb Esser
I keep reading things saying "Object.keys is much faster than for..in", but in most tests I do (and in a bunch I find on jsperf.com), that is the opposite of the case. Object.keys seems to often be very fast compared to calling Object.hasOwnProperty on each element inside of a for..in, but it seem

Re: [nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-13 Thread Axel Kittenberger
I still dislike forEach() same as some(); since the semantics are just more complicated. break doesn work (ok return true), but named break is gone, continue is now called return, and named continue is gone, and you cannot return from the parent function, which often I do to shortcut. You lose so m

[nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-13 Thread Shimon Doodkin
My post mainly about loops, not about prototypes. To not use prototypes when you write a plugin script to be integrated into 3rd parity website is a very good idea. Generaly Node.js is very controlled-settings environment. I like to add some agile convenience by using prototypes, I add them to to

Re: [nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-13 Thread Tim Caswell
On Mon, Feb 13, 2012 at 11:21 AM, Axel Kittenberger wrote: > some() is only for Arrays tough. Same with forEach. I had forgotten about Array.prototype.some(). If you want the short-circuit of some, use my code from above, but call it "some" to avoid confusion. > On Mon, Feb 13, 2012 at 5:47 PM

Re: [nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-13 Thread Axel Kittenberger
some() is only for Arrays tough. On Mon, Feb 13, 2012 at 5:47 PM, substack wrote: > On Feb 13, 7:01 am, Axel Kittenberger wrote: >> Why they didn't make the standard forEach in that returning false >> terminates the loop is beyond me... > > You can just abuse the short-circuiting of [].some: > >

[nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-13 Thread substack
On Feb 13, 7:01 am, Axel Kittenberger wrote: > Why they didn't make the standard forEach in that returning false > terminates the loop is beyond me... You can just abuse the short-circuiting of [].some: > [ 1, 2, 3, 4, 5, 6, 7 ].some(function (x) { console.log(x); if (x > 4) return > true }) 1

[nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-13 Thread Thom Blake
Well, in < ES4 that is. On Feb 13, 9:43 am, Thom Blake wrote: > > Or if you do make them DontEnum. > > DontEnum is not a property that can be set by the user. > > On Feb 12, 3:09 pm, Marcel Laverdet wrote: > > > > > > > > > This is one of those old Crockfordisms that I definitely don't agree wit

[nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-13 Thread Thom Blake
> Or if you do make them DontEnum. DontEnum is not a property that can be set by the user. On Feb 12, 3:09 pm, Marcel Laverdet wrote: > This is one of those old Crockfordisms that I definitely don't agree with. > How about instead you just don't add garbage to Object.prototype? Or if you > do ma

[nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-12 Thread Andy
Most of you probably know this, but sometimes I encounter modules that don't do this. And they do unexpected errors. Now that you've discovered Crockford, you should discover when to ignore crockford -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wi

Re: [nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-12 Thread Joe Developer
On Sun, Feb 12, 2012 at 8:50 PM, bradley griffiths < bradley.griffi...@gmail.com> wrote: > I normally do: > > Object.keys(myobject).forEach(function(key){ > myobject[key]; > }); > > Is this a bad way to do it? > In environments that support it, it seems like one of the very fastest approaches.

[nodejs] Re: how to write for-each loop in JavaScript properly

2012-02-12 Thread bradley griffiths
I normally do: Object.keys(myobject).forEach(function(key){ myobject[key]; }); Is this a bad way to do it? -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the