admin管理员组

文章数量:1431391

I was going through js-perf test cases and found this .

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

I was going through js-perf test cases and found this http://jsperf./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

Share Improve this question edited Oct 10, 2013 at 10:27 Exception asked Oct 10, 2013 at 10:15 ExceptionException 8,38924 gold badges88 silver badges141 bronze badges 10
  • 1 I don't see any result there that would say one of them is faster? – Zathrus Writer Commented Oct 10, 2013 at 10:19
  • what is your test. code?? – Arun Aravind Commented Oct 10, 2013 at 10:20
  • 1 I also think the difference is so minor that benchmark may not give the correct result. – VisioN Commented Oct 10, 2013 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 pletely 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 Commented Oct 10, 2013 at 10:22
  • @ZathrusWriter My doubt was is there any specific algorithm followed when creating of arrays while execution. – Exception Commented Oct 10, 2013 at 10:25
 |  Show 5 more ments

5 Answers 5

Reset to default 5

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

My Test: http://jsperf./ideaferid

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./pre-allocated-arrays-2/5
we can see that "100K is faster than 100K-1"... provided it's in first position.

It's not technically faster. Just the oute 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.

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:

-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)

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.

本文标签: javascriptWhy Array(99999) is slower than Array(100000)Stack Overflow