inarraytest
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var arr=[0,1,2,3,4,5,6,7,8,9];
function inArray(elem, array, i) {
var len;
if ( array ) {
if ( array.indexOf ) {
return array.indexOf.call( array, elem, i );
}
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;
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
inArray last |
|
pending… |
for loop last |
|
pending… |
inArray middle |
|
pending… |
for loop middle |
|
pending… |
inArray first |
|
pending… |
for loop first |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published
- Revision 2: published by T.J. Crowder
- Revision 3: published by T.J. Crowder
- Revision 4: published
- Revision 5: published
0 comments