$.inArray, _.indexOf, and a simple loop
JavaScript performance comparison
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
var r;
var array = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '11', '12', '33', '44', '55', '66', '77', '88', '99'];
function contains(array, value) {
var index = -1,
length = array.length;
while (++index < length) {
if (array[index] === value) {
return true;
}
}
return false;
}
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
$.inArray |
|
pending… |
_.indexOf |
|
pending… |
Simple loop |
|
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 by robertm and last updated
- Revision 2: published by Glauber and last updated
- Revision 3: published by Blaise Kal
- Revision 4: published
- Revision 5: published by John Factorial
- Revision 6: published
0 comments