0

I'm a javascript newbie, and I can't figure out why the variable o exists in the following code:

var data = [ {"name":"alice", "group":1}, {"name":"bob", "group":2} ];

function getGroup(n) { return n.group; }

function network(nodes, index) {
    var gc = {};
    nodes.forEach(function(n) {
        var i = index(n), o;
        if (condition) {
            o = gc[i] || (gc[i] = {x:0,y:0,count:0});
            o.x += n.x;
            o.y += n.y;
            o.count += 1;
        }
    });
}

var net = network(nodes, getGroup)

It seems to me that the iterator in the network function would be better written this way:

function network(data, index) {
    var gc = {};
    nodes.forEach(function(n) {
        var i = index(n);
        if (condition) {
            gc[i] = gc[i] || (gc[i] = {x:0,y:0,count:0});
            gc[i].x += n.x;
            gc[i].y += n.y;
            gc[i].count += 1;
        }
    });
}

I think the second loop is equivalent and it eliminates a variable (and it's easier for me to read). Is the first version following some recommended practice in javascript? I can imagine that it would be good practice to have var o; inside the iterator to reduce its scope, but in this case o only refers to an object that's already referenced by gc[i]. Why not just eliminate it?

shortorian
  • 1,082
  • 1
  • 10
  • 19
  • 1
    Please rephrase your question. I do not think it should contain "ANSWER", as that is the purpose of this site (Q&A). Also, rephrase it in a way that talks about caching and performance, as right now the title looks like a debug question. And why is conventions a tag? What about caching? – onebree Jul 23 '15 at 20:28
  • I originally wanted some discussion about conventions but it didn't go like that; edits on the way – shortorian Jul 23 '15 at 20:29
  • SO is not best about open-ended discussions for conventions. That would violate and lead to a flag for "too broad" or "primarily opinion-based." – onebree Jul 23 '15 at 20:31
  • 1
    I get that open ended discussion is bad, but I don't think "is there a primary source for best practices like the python PEPs" (which I've now deleted) is an open ended question. – shortorian Jul 23 '15 at 20:47
  • 2
    That may not be open-ended, but it runs up against another rule (#4 from [here](http://stackoverflow.com/help/on-topic)): _"Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow"_ Stick to asking about the programming problem itself. – Ted Hopp Jul 23 '15 at 21:07
  • 1
    As far as primary sources, you can look through [my search query for JS style guides](https://duckduckgo.com/?q=javascript+style+guide+github&t=fedora&ia=software&iai=0). Mozilla Dev Network has great guides on web technologies. Also, great books are Eloquent JS. For other resrouces, check [here](https://github.com/Michael0x2a/curated-programming-resources/blob/master/resources.md) and [here](https://github.com/vhf/free-programming-books). – onebree Jul 23 '15 at 21:11
  • Excellent, thanks a bunch. – shortorian Jul 23 '15 at 21:18
  • 1
    You shouldn't edit your question with a new question. If you have a new question, you should post a new question. You've already accepted an answer, if you change the question the posted answers makes no sense. Also, you won't be able to pick a new answer for your new question. – Jan Jul 23 '15 at 22:41
  • Jan, see comments above for the rationale for some edits. I can change the accepted answer whenever I want. By rolling back to the original question, you also removed some edits not related to your comment. – shortorian Jul 23 '15 at 22:55
  • @dshort - you shouldn't change the question in such a way that invalidates the existing answers. If you have a follow up question ask it as a **new** question. – ChrisF Jul 23 '15 at 23:07

4 Answers4

3

Likely because g[i] requires indexing into an array repeatedly looking up a property of an object, whereas storing the value in o caches the value once and provides for faster access subsequently.

Shripathi Kamath
  • 310
  • 3
  • 10
  • Since you're storing the object in `gc[i]` and then storing the object reference in `o`, you're acting on the same object and it ends up being exactly the same as your code. Only more efficient. – Jan Jul 23 '15 at 01:35
  • Actually, `gc` is not an array, so this is not array index but simple property look-up. – Ted Hopp Jul 23 '15 at 02:14
  • Could you (or even @TedHopp) elaborate and correct that mistake in this answer? Also add some code formatting – onebree Jul 23 '15 at 20:29
1

This is a (minor) performance enhancement. It replaces three subscripting operations with a local variable assignment and three local variable accesses. I have my doubts whether the performance improvement would be noticeable. See this thread for more info on the performance of various array operations. (Actually, that info is somewhat irrelevant because gc is an object, not an array. These are plain old property look-ups being saved, not array indexing, so the performance increase might be a little more significant.)

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

Even simpler

change

        gc[i] = gc[i] || (gc[i] = {x:0,y:0,count:0});

to

        gc[i] = gc[i] || {x:0,y:0,count:0};
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0

Because it has to keep re-seeking the item in the array via the index. o is storing the reference to the location in memory so it's quicker to access later.

wayofthefuture
  • 8,339
  • 7
  • 36
  • 53