String In Array
JavaScript performance comparison
Info
Tests for fastest way to determine if a string is in an array
Preparation code
<script>
function asObject(a)
{
var o = {};
for (var i = 0; i < a.length; i++)
{
o[a[i]] = '';
}
return o;
}
</script>
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
var testArray = ['a', 'b', 'c', 'd', 'e'];
var testObject = { 'a': '', 'b': '', 'c': '', 'd': '', 'e': ''};
var testFor = 'f';
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Switch |
|
pending… |
In Array (foreach style) |
|
pending… |
In Map |
|
pending… |
Convert Array to Object |
|
pending… |
Array (for loop style) |
|
pending… |
Key exists (undefined) |
|
pending… |
Key Exists |
|
pending… |
Underscore Index Of |
|
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 Dom
- Revision 2: published
0 comments