6

Why is v1 so much slower than v2?

v1 --

var foo = function (a,b,c) { 
 this.a=a; this.b=b; this.c=c; 
}
var pcs = new Array(32);
for (var n=32; n--;) {
 ref = new foo(1,2,3)
 pcs[n] = ref; //*****
}

v2 --

var foo = function (a,b,c) { 
 this.a=a; this.b=b; this.c=c; 
}
var pcs = new Array(32);
for (var n=32; n--;) {
 ref = new foo(1,2,3)
 pcs[n] = 1; //*****
}

I figured that since I'm holding a reference to the new object in 'ref', that simply assigning that reference to an element in the array would be about as fast as assigning a literal value, but it turns out that assigning the reference is considerably slower. Can anyone shed some light on this? Anything I can do to improve the performance here on V1?

Fiddle:

http://jsfiddle.net/a0kw9rL1/1/

Paddy
33.9k15 gold badges82 silver badges121 bronze badges
asked May 12, 2015 at 14:47
10
  • where loop terminating condition?? Commented May 12, 2015 at 14:55
  • @ozil: Where it always is, the second part of the for "header", n--. If it returns 0, the loop terminates. Commented May 12, 2015 at 14:58
  • @ Felix Kling what it is not decrements after 0 Commented May 12, 2015 at 15:00
  • You've basically done your best. If you need to save a reference (which contains more data than a mere number), that's all you can do. On the other hand, ask yourself why you need to improve performances in such a task. Obviously these snippets are proofs of concept, so only you know what's behind. Commented May 12, 2015 at 15:01
  • 3
    Difference for me is negligible. Not enough to determine a literal difference between v1 and v2: jsperf.com/array-reference-assignment Commented May 12, 2015 at 15:13

1 Answer 1

3

simply assigning that reference to an element in the array would be about as fast as assigning a literal value

Yes, it basically is1. However, allocating an object probably makes the difference here.
In V2, ref is only allocated once and repeatedly overwritten, it might be allocated on the stack not on the heap, and dead code elimination might even completely optimise it away.
In V1, ref needs to be allocated on the heap, and repeatedly in a new location, as all the different instances are accessible from pcs.

V1 is just eating more memory than V21. However, due to your very small array, the difference is neglible. If you use really large one, you can spot the difference: http://jsperf.com/array-reference-assignment/3

[1]: Well, for some reason not really. But I can't explain that, except garbage collection is different when you profile memory usage

answered May 12, 2015 at 15:49

8 Comments

I think this is the right answer except 'V1' and 'V2' are reversed in it's current version. Possibly making that update (if you agree with me) would get this accepted.
@Bergi "it might be allocated on the stack not on the heap" Can describe "stack" , "heap" ; differences between the two ? , for a viewer that may not have a lay reference point for those terms within computer science ?
@guest271314: stack vs heap.
Were you able to reproduce the performance difference reported by the OP? One of the comments posted a link showing no perf difference (jsperf.com/array-reference-assignment) and I noticed the same thing running with the console. I'm wondering how the OP saw a performance difference between the two versions in the first place.
@GabrielSouthern: Yes, see the jsperf links in my answers, that are based on RGrahams version. However, indeed the differences are neglible for the size-32-arrays.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.