2

I was going through js-perf test cases and found this http://jsperf.com/pre-allocated-arrays-2/2.

In the result Array(100000) is faster than Array(99999). Why like this?

Edit:

My doubt was is there any specific algorithm followed when creating of arrays while execution. Even when I am executing 99 vs 100. 100 was faster in my browser. To get an idea about the algorithm if any is there, I posted this question

Browser : Chrome 30.0.15 OS : Mac OSX

Exception
  • 8,111
  • 22
  • 85
  • 136
  • 1
    I don't see any result there that would say one of them is faster? – Zathrus Writer Oct 10 '13 at 10:19
  • what is your test. code?? – Arun Aravind Oct 10 '13 at 10:20
  • 1
    I also think the difference is so minor that benchmark may not give the correct result. – VisioN Oct 10 '13 at 10:20
  • 1
    @ZathrusWriter: he tested in Chrome. Other users (like me) also tried the test in Firefox 24 and Safari 6.1. They were faster in multitudes which caused the Chrome results to completely vanish in graph. If you check the source or zoom in far enough, you'll see them. Exception: please explicitly mention exact browser make and version used and the results directly in the question. JS performance is browser make/version specific. The difference is by the way more extreme in Safari 6.1. – BalusC Oct 10 '13 at 10:22
  • @ZathrusWriter My doubt was is there any specific algorithm followed when creating of arrays while execution. – Exception Oct 10 '13 at 10:25
  • The margin of the difference isn't significant, it could well be because of how the test is run, the results are an approximation, it's not an exact science. – Evan Trimboli Oct 10 '13 at 10:27
  • @BalusC Please look at my edit in question – Exception Oct 10 '13 at 10:27
  • You forgot to tell the Chrome version. As you can see in the current result graph of your jsperf benchmark, there are at least 3 different Chrome versions. – BalusC Oct 10 '13 at 10:29
  • @BalusC updated the question with chrome version I am using. – Exception Oct 10 '13 at 10:31
  • just reverse the test order, and you get reverse result. See my answer below. – GameAlchemist Oct 10 '13 at 11:48

5 Answers5

5

This tool doesn't give %100 accurate results so it is normal Array(100000) is faster than Array(99999).

My Test: http://jsperf.com/ideaferid

enter image description here

Farid Movsumov
  • 12,350
  • 8
  • 71
  • 97
3

It's not technically faster. Just the outcome of your tests came out that way.

I ran the tests too and Array(99999) was 0.11% faster than the Array(100000). Run it again without doing anything while it's testing. By the way there is an approximation - when I ran it had +/- 0.20%.. That's why it could take the slightly larger one to run faster.

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
Dropout
  • 13,653
  • 10
  • 56
  • 109
3

This mystery is linked to garbage collection.
Running the first test will create garbage -a lot in fact-, so garbage collector is bound to run at some time, slowing down execution.
The second test will suffer from the garbage created in the first one, which make it more likely than the first test to be slower.
Try to swap the tests, and you'll see that it is the first one that wins : the question is not about the array size, but rather test order.

We see, here, one of the bias induced by jsperf, which results should always be used with precautions.

i reversed the test order here :
http://jsperf.com/pre-allocated-arrays-2/5
we can see that "100K is faster than 100K-1"... provided it's in first position.

GameAlchemist
  • 18,995
  • 7
  • 36
  • 59
1

Some implementations (notably Safari's Nitro) do pre-allocate memory when the Array constructor is called with a length, other's don't. Probably 100k is some arbitrarily chosen limit at which they switch to a different behavior.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    *"Some implementations do pre-allocate memory when the Array constructor is called with a length, other's don't."* Do you have any proof of that? – VisioN Oct 10 '13 at 10:27
  • @Bergi I even observed 100 is faster than 99. Wanted to know if is there any behaviour change for values. And thanks for an answer. – Exception Oct 10 '13 at 10:30
  • @VisioN: http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/ (`Ctrl+F` pre-allocate) – Bergi Oct 10 '13 at 10:37
  • @Bergi Hm... The test in the article is comparing *"empty literal versus pre-allocated array in various browsers"* and not how different browsers implement pre-allocated array. – VisioN Oct 10 '13 at 10:46
  • @VisioN: Yeah, it's only stating *that* some engines implement preallocation. If you're looking for the actual implementation, check their source codes :-) – Bergi Oct 10 '13 at 10:56
  • 1
    @Bergi Ahah, yeah.. Started to dig V8. Be back in 2 days :) – VisioN Oct 10 '13 at 10:59
1

Your answer lies within tests structure. While it is a blackbox, I can not say why it is so, but in javascript first code will be faster.

I've used that testing system and got results:

enter image description here

-as you can see, second code was slower. As a conclusion - it is either unstable test system or not enough iterations to evaluate meaningful execution time value. Also note, that, despite my browser in not Chrome, if the reason was inside js, this should not matter (while my result is obvious)

Alma Do
  • 37,009
  • 9
  • 76
  • 105
  • As different browsers use different JS engines, it will likely matter on which browser you use. – adamjc Oct 10 '13 at 10:48