jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
We test the speed of member access with various ways of creating objects.
<script>
function A() {
var a = 0;
var b = 0;
var c = 0;
var d = 0;
this.m1 = function() {
// avoid bug in Chrome 10.x that will optimize away the `a` reference
if (typeof a == 'number') {
a += 1;
b += 2;
c += 3;
d += a;
}
};
this.m2 = function() {
a += 4;
b += 5;
c += 6;
d += b;
};
this.m3 = function() {
a += 7;
b += 8;
c += 9;
d += c;
};
this.m = function() {
this.m1();
this.m2();
this.m3();
};
}
function B() {
this.a = 0;
this.b = 0;
this.c = 0;
this.d = 0;
this.m1 = function() {
this.a += 1;
this.b += 2;
this.c += 3;
this.d += this.a;
};
this.m2 = function() {
this.a += 4;
this.b += 5;
this.c += 6;
this.d += this.b;
};
this.m3 = function() {
this.a += 7;
this.b += 8;
this.c += 9;
this.d += this.c;
};
this.m = function() {
this.m1();
this.m2();
this.m3();
};
}
// assigning the whole prototype
function C() {}
C.prototype = {
a: 0,
b: 0,
c: 0,
d: 0,
m1: function() {
this.a += 1;
this.b += 2;
this.c += 3;
this.d += this.a;
},
m2: function() {
this.a += 4;
this.b += 5;
this.c += 6;
this.d += this.b;
},
m3: function() {
this.a += 7;
this.b += 8;
this.c += 9;
this.d += this.c;
},
m: function() {
this.m1();
this.m2();
this.m3();
}
};
// assigning the prototype
function D() {}
D.prototype.a = 0;
D.prototype.b = 0;
D.prototype.c = 0;
D.prototype.d = 0;
D.prototype.m1 = function() {
this.a += 1;
this.b += 2;
this.c += 3;
this.d += this.a;
};
D.prototype.m2 = function() {
this.a += 4;
this.b += 5;
this.c += 6;
this.d += this.b;
};
D.prototype.m3 = function() {
this.a += 7;
this.b += 8;
this.c += 9;
this.d += this.c;
};
D.prototype.m = function() {
this.m1();
this.m2();
this.m3();
};
// assigning the whole prototype, but members in constructors
function E() {
this.a = 0;
this.b = 0;
this.c = 0;
this.d = 0;
}
E.prototype = {
m1: function() {
this.a += 1;
this.b += 2;
this.c += 3;
this.d += this.a;
},
m2: function() {
this.a += 4;
this.b += 5;
this.c += 6;
this.d += this.b;
},
m3: function() {
this.a += 7;
this.b += 8;
this.c += 9;
this.d += this.c;
},
m: function() {
this.m1();
this.m2();
this.m3();
}
};
// object literal
function F() {
return {
a: 0,
b: 0,
c: 0,
d: 0,
m1: function() {
this.a += 1;
this.b += 2;
this.c += 3;
this.d += this.a;
},
m2: function() {
this.a += 4;
this.b += 5;
this.c += 6;
this.d += this.b;
},
m3: function() {
this.a += 7;
this.b += 8;
this.c += 9;
this.d += this.c;
},
m: function() {
this.m1();
this.m2();
this.m3();
}
};
}
var oa = new A();
var ob = new B();
var oc = new C();
var od = new D();
var oe = new E();
var of = new F();
// increase default run time of benchmarks
Benchmark.prototype.MAX_TIME_ELAPSED = 15;
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
A |
| ready |
B |
| ready |
C |
| ready |
D |
| ready |
E |
| ready |
F |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.