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
2D vector class as closures vs traditional constructors
// closure class
function createVector(x, y) {
var coords = Object.seal({ x: x, y: y });
function scale(factor) {
coords.x *= factor;
coords.y *= factor;
}
function addVector(anotherVector) {
coords.x += anotherVector.getX();
coords.y += anotherVector.getY();
}
function getX() { return coords.x }
function getY() { return coords.y }
return Object.freeze({
getX: getX,
getY: getY,
scale: scale,
addVector: addVector
});
};
// traditional constructor and prototype
function Vector(x, y) {
this.x = x;
this.y = y;
Object.seal(this);
};
Vector.prototype.scale = function(factor) {
this.x *= factor;
this.y *= factor;
};
Vector.prototype.addVector = function(anotherVector) {
this.x += anotherVector.getX();
this.y += anotherVector.getY();
};
Vector.prototype.getX = function() { return this.x; };
Vector.prototype.getY = function() { return this.y; };
Object.freeze(Vector.prototype);
Object.freeze(Vector);
Ready to run.
Test | Ops/sec | |
---|---|---|
Create closure |
| ready |
Create instance |
| ready |
Use closure |
| ready |
Use instance |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.