arrays with and without holes
JavaScript performance comparison
Info
Is it better to spend time up front constructing arrays of objects without holes? Or to just check for holes when you iterate?
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
var simple = [], complex = [],
selectIndexes = [1, 2, 10, 14, 300, 1000],
pairs = [
{ "id" : 1,
"name" : "Albert"
},
{ "id" : 2,
"name" : "Bailey"
},
{ "id" : 4,
"name" : "Charlotte"
},
{ "id" : 5,
"name" : "Darlene"
},
{ "id" : 10,
"name" : "Edna"
},
{ "id" : 12,
"name" : "Faron"
},
{ "id" : 13,
"name" : "Gary"
},
{ "id" : 14,
"name" : "Helen"
},
{ "id" : 15,
"name" : "Igor"
},
{ "id" : 16,
"name" : "Justin"
},
{ "id" : 17,
"name" : "Kyle"
},
{ "id" : 300,
"name" : "Lynette"
},
{ "id" : 500,
"name" : "Morgan"
},
{ "id" : 1000,
"name" : "Nora"
}
],
get = function (a, i) {
var ret = null;
$.each(a, function () {
if (this.id === i.valueOf()) {
ret = this;
}
});
return ret;
};
};
Benchmark.prototype.teardown = function() {
simple = [], complex = [];
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
simple |
|
pending… |
complex |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments