monomorphism vs polymorphism
JavaScript performance comparison
Preparation code
<script>
var UID = 0;
</script>
<script>
Benchmark.prototype.setup = function() {
// Need to generate core of the benchmark dynamically to ensure
// that type feedback does not leak between individual sample runs.
var code = " /* " + (UID++) + " */ " + " for (var i = 0; i < N; i++) {" +
" var obj = arr[i];" +
" result += obj.x + obj.y + i;" +
"}" +
"return result";
var f = new Function("N, arr, result", code);
var K = 10;
var objs = [];
for (var i = 0; i < (K / 2); i++) {
objs.push(new function () {
this.x = 0;
this.y = 0;
} ());
}
for (var i = 0; i < (K / 2); i++) {
objs.push(new function () {
this.y = 0;
this.x = 0;
} ());
}
var mono = [],
poly = [],
poly2 = [],
mega = [];
var N = 20;
for (var i = 0; i < N; i++) {
mono.push(objs[0])
poly.push(objs[i % 3])
poly2.push(objs[(i % 3) || (K / 2)])
mega.push(objs[i % 6])
}
var result = 0;
};
Benchmark.prototype.teardown = function() {
if (result < 0) throw "";
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
calling monomorphic function
|
|
pending… |
calling polymorphic
|
|
pending… |
calling megamorphic
|
|
pending… |
poly (different property offsets)
|
|
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.
- Revision 1: published Pablo
- Revision 2: published
- Revision 3: published elkorn
- Revision 6: published
- Revision 10: published
- Revision 11: published
- Revision 12: published John Smith
- Revision 13: published
0 Comments