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
Some concepts to loop through an whole array of objects (non-literal).
<script>
//
var arr = [];
var obj = {};
var sparse = [];
// create arrays
for(var i = 0; i < 10000; ++i) {
arr.push({ payload: i });
obj[i] = { payload: i };
}
// create sparsed array
sparse [0] = "a";
sparse [10] = "b";
sparse [9999] = "c";
// cached keys
var keys = Object.keys(obj);
Object.prototype.getKeys = function(){ return this.constructor.keys(this); };
Object.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
var breaker = {};
Object.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;
}
}
};
// 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;
}
}
};
// object array to simple array
Object.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
Object.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
Object.prototype.getValuesIndexed = function() {
var obj = this, len = obj.getKeys().length, values = [];
for(var i = 0; i < len; ++i) values[i] = obj[i];
return values;
};
//console.log(obj.getValuesIndexed());
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
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 Prototype |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.