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
<script>
// test arrays
var arr = [];
var obj = {};
var obj2 = new Object();
var obj3 = new (function(){
var $obj3 = function(){
this.obj = (function(){
var obj3 = {};
for(var i = 0; i < 10000; ++i) {
obj3[i] = { payload: i };
}
return obj3;
}());
};
$obj3.prototype.getSum = function() {
var obj = this, sum = 0;
for(var key in obj) {
if(obj.hasOwnProperty(key)) sum += obj[key].payload;
return sum;
}
};
return $obj3;
}());
var sparse = [];
var breaker = {};
//
obj2.constructor.prototype.getKeys = function(){ return Object.keys(this); };
obj2.constructor.prototype.getSum = function(){
var obj = this, sum = 0, len = obj.getKeys().length;
for(var i = 0; i < len; ++i) sum += obj[i].payload;
return sum;
};
// forEach for assoziative arrays
obj2.constructor.prototype.each = function(obj, iterator, context) {
//var key; //var obj = this;
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
if(iterator.call(context, obj[key], key, obj) === breaker) return;
}
}
};
// object array to simple array
obj2.constructor.prototype.getValuesPush = function() {
var obj = this, values = [];
for(var key in obj) if(obj.hasOwnProperty(key)) values.push(obj[key]);
return values;
};
// object array to simple array
obj2.constructor.prototype.getValuesIterate = function() {
var obj = this, values = [], len = 0;
for(var key in obj) if(obj.hasOwnProperty(key)) values[len++] = obj[key];
return values;
};
// object array to simple array
obj2.constructor.prototype.getValuesIndexed = function() {
var obj = this, len = obj.getKeys().length, values = [];
for(var i = 0; i < len; ++i) values[i] = obj[i];
return values;
};
// forEach for assoziative arrays
var each = function(obj, iterator, context) {
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
if(iterator.call(context, obj[key], key, obj) === breaker) return;
}
}
};
// create arrays
for(var i = 0; i < 10000; ++i) {
arr.push({ payload: i });
obj[i] = { payload: i };
obj2[i] = { payload: i };
obj3[i] = { payload: i };
}
// create sparsed array
sparse [0] = "a";
sparse [10] = "b";
sparse [9999] = "c";
// cached keys
var keys = Object.keys(obj);
//console.log(obj.getValuesIndexed());
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Internal Routine via Function Prototype |
| ready |
Simple Array [ ] of Objects |
| ready |
Object Array { } of Objects |
| ready |
Object Iterate (known length) |
| ready |
Object > keys() Iterate |
| ready |
Array.foreach() |
| ready |
Object > keys().foreach() |
| ready |
Using Array.reduce() |
| ready |
Object.foreach() Polyfill |
| ready |
Object > Array.foreach() Prototype Push |
| ready |
Object > Array.foreach() Prototype Iterate |
| ready |
Object > Array.foreach() Prototype Indexed |
| ready |
Object > Keys.foreach() non-cached |
| ready |
Object.foreach() Prototype Polyfill |
| ready |
Internal Routine via Object Prototype |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.