I ran some tests, and the data point that the jQuery inArray()
is much slower than a simple loop.
And array.indexOf()
is not even on the tests because I previously did other tests, and it performed even worse.
- Why it is much slower?
- Why don't they use simple loops?
- Is there something that I am overseeing?
There must be a good reason for not using this:
for(var i=0,len=arr.length,rtn=-1;i<len;i++){
if(arr[i]==="arritem"){
rtn=i;
break;
}
}
I ran some tests, and the data point that the jQuery inArray()
is much slower than a simple loop.
And array.indexOf()
is not even on the tests because I previously did other tests, and it performed even worse.
- Why it is much slower?
- Why don't they use simple loops?
- Is there something that I am overseeing?
There must be a good reason for not using this:
for(var i=0,len=arr.length,rtn=-1;i<len;i++){
if(arr[i]==="arritem"){
rtn=i;
break;
}
}
Share
Improve this question
asked Jan 13, 2012 at 23:30
mithril333221mithril333221
8192 gold badges10 silver badges20 bronze badges
6
-
From your jsperf test:
return array.indexOf.call( array, elem, i );
??? Why notreturn array.indexOf(elem, i);
? Why the extra layer going throughcall
? – T.J. Crowder Commented Jan 13, 2012 at 23:33 - @T.J.Crowder I thought jQuery was using that, I copied it from stackoverflow./a/8856637/1148349 – mithril333221 Commented Jan 13, 2012 at 23:36
- @mithril333221 it seems it is, code.jquery./jquery-1.7.1.js – ajax333221 Commented Jan 13, 2012 at 23:42
-
If your goal is to test jQuery's
inArray
, why not test jQuery'sinArray
? You can include libraries in jsperf tests. – T.J. Crowder Commented Jan 13, 2012 at 23:42 -
1
@ajax333221: No, that's not quite the same thing. In the jQuery source, it's referencing
indexOf
(a free symbol); in the OP's test case, it's referencingarray.indexOf
(a property ofarray
). – T.J. Crowder Commented Jan 13, 2012 at 23:44
2 Answers
Reset to default 5If you're going to test jQuery's inArray
, actually test jQuery's inArray
, and pare apples to apples (calling a function to calling a function — you can write the loop inline in places where performance is hugely critical, but it wouldn't be your default move, presumably): http://jsperf./inarraytest/3
Preparation HTML:
<script src="//ajax.googleapis./ajax/libs/jquery/1/jquery.min.js"></script>
<script>
Preparation code:
var arr=[0,1,2,3,4,5,6,7,8,9];
function arrayLoop(elem, array, i) {
var len = array.length;
i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0;
for ( ; i < len; i++ ) {
// Skip accessing in sparse arrays
if ( i in array && array[ i ] === elem ) {
return i;
}
}
return -1;
}
Tests:
// jQuery.inArray last
var rtn=jQuery.inArray(9,arr);
// arrayLoop last
var rtn = arrayLoop(9,arr);
// jQuery.inArray middle
var rtn=jQuery.inArray(4,arr);
// arrayLoop middle
var rtn = arrayLoop(4,arr);
// jQuery.inArray first
var rtn=jQuery.inArray(0,arr);
// arrayLoop first
var rtn = arrayLoop(0,arr);
Results on Chrome (which has indexOf
) are that jQuery.inArray
is always faster than arrayLoop
(in the first pair of test cases, where we're searching for the last entry, dramatically so).
Results on IE6 (which doesn't have indexOf
): jQuery.inArray
is always faster than arrayLoop
, though unsurprisingly not by much (as it has to do essentially the same work) — except, curiously, in the case where we're searching for the first entry in the array, in which case it's much faster.
What you are doing is just a part of the same jQuery $.inArray code and of course it will be faster when you take a snippet of the code and test that functionality alone. Check all the conditions that are validated before it actually iterates through the list to look for the element.
That is the extra time between the simple loop and $.inArray().
Finally: You can stick to the simple loop if you know the following for sure,
- Input is always an array
- Option of sending the start Index to make your search faster.
- Use of native browser indexOf function.