find element in obj vs array
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var obj = {},
arr = [],
count = 0;
for (var i = 0; i < 1000; i++) {
arr.push(i);
obj[i] = 1;
}
function in_array(needle, haystack) {
for (var i = 0, maxi = haystack.length; i < maxi; ++i) {
if (haystack[i] == needle) {
return true;
}
}
return false;
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
find in array (1) |
|
pending… |
find in array (5) |
|
pending… |
find in array (20) |
|
pending… |
find in obj (1) |
|
pending… |
find in obj (5) |
|
pending… |
find in obj (20) |
|
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
- Revision 3: published
- Revision 4: published by Rob Edgar
0 comments